home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / amyboard / xboard-3.3.pl0 / backend.c < prev    next >
C/C++ Source or Header  |  1995-08-12  |  168KB  |  6,241 lines

  1. /*
  2.  * backend.c -- Common back end for X and Windows NT versions of
  3.  * XBoard $Id: backend.c,v 1.55 1995/07/28 05:23:42 mann Exp $
  4.  *
  5.  * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
  6.  * Enhancements Copyright 1992-95 Free Software Foundation, Inc.
  7.  *
  8.  * The following terms apply to Digital Equipment Corporation's copyright
  9.  * interest in XBoard:
  10.  * ------------------------------------------------------------------------
  11.  * All Rights Reserved
  12.  *
  13.  * Permission to use, copy, modify, and distribute this software and its
  14.  * documentation for any purpose and without fee is hereby granted,
  15.  * provided that the above copyright notice appear in all copies and that
  16.  * both that copyright notice and this permission notice appear in
  17.  * supporting documentation, and that the name of Digital not be
  18.  * used in advertising or publicity pertaining to distribution of the
  19.  * software without specific, written prior permission.
  20.  *
  21.  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  22.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  23.  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  24.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  25.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  26.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  27.  * SOFTWARE.
  28.  * ------------------------------------------------------------------------
  29.  *
  30.  * The following terms apply to the enhanced version of XBoard distributed
  31.  * by the Free Software Foundation:
  32.  * ------------------------------------------------------------------------
  33.  * This program is free software; you can redistribute it and/or modify
  34.  * it under the terms of the GNU General Public License as published by
  35.  * the Free Software Foundation; either version 2 of the License, or
  36.  * (at your option) any later version.
  37.  *
  38.  * This program is distributed in the hope that it will be useful,
  39.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  41.  * GNU General Public License for more details.
  42.  *
  43.  * You should have received a copy of the GNU General Public License
  44.  * along with this program; if not, write to the Free Software
  45.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  46.  * ------------------------------------------------------------------------
  47.  *
  48.  * See the file ChangeLog for a revision history.  */
  49.  
  50. #include <config.h>
  51.  
  52. #include <stdio.h>
  53. #include <ctype.h>
  54. #include <errno.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <math.h>
  58.  
  59. #if STDC_HEADERS
  60. # include <stdlib.h>
  61. # include <string.h>
  62. #else /* not STDC_HEADERS */
  63. # if HAVE_STRING_H
  64. #  include <string.h>
  65. # else /* not HAVE_STRING_H */
  66. #  include <strings.h>
  67. # endif /* not HAVE_STRING_H */
  68. #endif /* not STDC_HEADERS */
  69.  
  70. #if HAVE_SYS_FCNTL_H
  71. # include <sys/fcntl.h>
  72. #else /* not HAVE_SYS_FCNTL_H */
  73. # if HAVE_FCNTL_H
  74. #  include <fcntl.h>
  75. # endif /* HAVE_FCNTL_H */
  76. #endif /* not HAVE_SYS_FCNTL_H */
  77.  
  78. #if TIME_WITH_SYS_TIME
  79. # include <sys/time.h>
  80. # include <time.h>
  81. #else
  82. # if HAVE_SYS_TIME_H
  83. #  include <sys/time.h>
  84. # else
  85. #  include <time.h>
  86. # endif
  87. #endif
  88.  
  89. #if defined(_amigados) && !defined(__GNUC__)
  90. struct timezone {
  91.     int tz_minuteswest;
  92.     int tz_dsttime;
  93. };
  94. extern int gettimeofday(struct timeval *, struct timezone *);
  95. #endif
  96.  
  97. #if HAVE_UNISTD_H
  98. # include <unistd.h>
  99. #endif
  100.  
  101. #include "common.h"
  102. #include "frontend.h"
  103. #include "backend.h"
  104. #include "parser.h"
  105. #include "moves.h"
  106. #ifdef ZIPPY
  107. # include "zippy.h"
  108. #endif
  109.  
  110.  
  111. /* A point in time */
  112. typedef struct {
  113.     long sec;  /* Assuming this is >= 32 bits */
  114.     int ms;    /* Assuming this is >= 16 bits */
  115. } TimeMark;
  116.  
  117.  
  118. int establish P((void));
  119. void read_from_player P((InputSourceRef isr, char *buf, int count, int error));
  120. void read_from_ics P((InputSourceRef isr, char *buf, int count, int error));
  121. void SendToICS P((char *s));
  122. void SendMoveToICS P((ChessMove moveType, int fromX, int fromY,
  123.               int toX, int toY));
  124. Boolean ParseMachineMove P((char *machineMove, int moveNum,
  125.                 ChessMove *moveType, int *fromX, int *fromY,
  126.                 int *toX, int *toY, char *promoChar));
  127. void InitPosition P((int redraw));
  128. void SendCurrentBoard P((ProcRef pr));
  129. void SendBoard P((ProcRef pr, Board board));
  130. void FinishUserMove P((ChessMove moveType, int toX, int toY));
  131. void HandleMachineMove P((char *message, InputSourceRef isr));
  132. void LoadGameLoop P((void));
  133. int LoadGameOneMove P((void));
  134. int LoadGameFromFile P((char *filename, int n, char *title, int useList));
  135. int LoadPositionFromFile P((char *filename, int n, char *title));
  136. int SaveGameToFile P((char *filename));
  137. int SavePositionToFile P((char *filename));
  138. void ApplyMove P((ChessMove *moveType, int fromX, int fromY,
  139.           int toX, int toY, Board board));
  140. void MakeMove P((ChessMove *moveType, int fromX, int fromY,
  141.          int toX, int toY));
  142. void BackwardInner P((int target));
  143. void ForwardInner P((int target));
  144. void GameEnds P((ChessMove result, char *resultDetails, int whosays));
  145. void Reset P((int redraw));
  146. void EditPositionDone P((void));
  147. void PrintOpponents P((FILE *fp));
  148. void PrintPosition P((FILE *fp, int move));
  149. void InitChessProgram P((char *hostName, char *programName,
  150.              ProcRef *pr, InputSourceRef *isr, int *sendTime));
  151. void SendToProgram P((char *message, ProcRef pr));
  152. void ReceiveFromProgram P((InputSourceRef isr, char *buf, int count, int error));
  153. void SendSearchDepth P((ProcRef pr));
  154. void SendTimeRemaining P((ProcRef pr));
  155. void SendMoveToProgram P((ChessMove moveType, int fromX, int fromY, int toX,
  156.               int toY, ProcRef firstProgramPR, int sendTime));
  157. void Attention P((ProcRef pr));
  158. void ResurrectChessProgram P((void));
  159. void DisplayComment P((int moveNumber, char *text));
  160. void DisplayMove P((int moveNumber));
  161.  
  162. void ParseGameHistory P((char *game));
  163. void ParseBoard12 P((char *string));
  164. void StartClocks P((void));
  165. void DisplayBothClocks P((void));
  166. void SwitchClocks P((void));
  167. void StopClocks P((void));
  168. void ResetClocks P((void));
  169. char *PGNDate P((void));
  170. void SetGameInfo P((void));
  171. char *PositionToFEN P((int move));
  172. Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
  173. int RegisterMove P((void));
  174. void MakeRegisteredMove P((void));
  175. void TruncateGame P((void));
  176. int looking_at P((char *, int *, char *));
  177. void CopyPlayerNameIntoFileName P((char **, char *));
  178. char *SavePart P((char *));
  179. int SaveGameOldStyle P((FILE *));
  180. int SaveGamePGN P((FILE *));
  181. char *StrSave P((char *));
  182. void GetTimeMark P((TimeMark *));
  183. long SubtractTimeMarks P((TimeMark *, TimeMark *));
  184. void CheckFlags P((void));
  185. long NextTickLength P((long));
  186. void CheckTimeControl P((void));
  187. void show_bytes P((FILE *, char *, int));
  188. char *CmailMsg P((void));
  189.  
  190. /* States for ics_getting_history */
  191. #define H_FALSE 0
  192. #define H_REQUESTED 1
  193. #define H_GOT_REQ_HEADER 2
  194. #define H_GOT_UNREQ_HEADER 3
  195. #define H_GETTING_MOVES 4
  196.  
  197. /* whosays values for GameEnds */
  198. #define GE_ICS 0
  199. #define GE_GNU 1
  200. #define GE_PLAYER 2
  201. #define GE_FILE 3
  202.  
  203. /* Maximum number of games in a cmail message */
  204. #define CMAIL_MAX_GAMES 20
  205.  
  206. /* Different types of move when calling RegisterMove */
  207. #define CMAIL_MOVE   0
  208. #define CMAIL_RESIGN 1
  209. #define CMAIL_DRAW   2
  210. #define CMAIL_ACCEPT 3
  211.  
  212. /* Different types of result to remember for each game */
  213. #define CMAIL_NOT_RESULT 0
  214. #define CMAIL_OLD_RESULT 1
  215. #define CMAIL_NEW_RESULT 2
  216.  
  217. /* Fake up flags for now, as we aren't keeping track of castling
  218.    availability yet */
  219. #define FakeFlags(index) \
  220.     (((((index) % 2) == 0) ? F_WHITE_ON_MOVE : 0) | F_ALL_CASTLE_OK)
  221.  
  222. FILE *gameFileFP, *fromUserFP, *toUserFP, *debugFP;
  223.  
  224. char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
  225. char bookOutput[MSG_SIZ], thinkOutput[MSG_SIZ];
  226.  
  227. int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0,
  228.   pauseExamForwardMostMove = 0,
  229.   nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0,
  230.   cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES],
  231.   cmailMsgLoaded = FALSE, cmailMailedMove = FALSE,
  232.   cmailOldMove = -1, firstMove = TRUE, flipView = FALSE,
  233.   blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE,
  234.   searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE,
  235.   whiteFlag = FALSE, blackFlag = FALSE, maybePondering = FALSE, 
  236.   ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE,
  237.   matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE,
  238.   cmailMoveType[CMAIL_MAX_GAMES];
  239. long ics_basetime, ics_increment = 0, ics_clock_paused = 0;
  240. ProcRef firstProgramPR = NoProc, secondProgramPR = NoProc, icsPR = NoProc,
  241.   lastMsgPR = NoProc, cmailPR = NoProc;
  242. InputSourceRef firstProgramISR = NULL, secondProgramISR = NULL,
  243.   telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
  244. int firstSendTime = 2, secondSendTime = 2;  /* 0=don't, 1=do, 2=test */
  245. char timeTestStr[MSG_SIZ];
  246. GameMode gameMode = BeginningOfGame, lastGameMode = BeginningOfGame;
  247. char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2],
  248.   ptyname[24];
  249. char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
  250.  
  251. long whiteTimeRemaining, blackTimeRemaining, timeControl;
  252. long timeRemaining[2][MAX_MOVES];
  253.      
  254. GameInfo gameInfo;
  255.  
  256. AppData appData;
  257.  
  258. Board boards[MAX_MOVES], initialPosition = {
  259.     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
  260.     WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
  261.     { WhitePawn, WhitePawn, WhitePawn, WhitePawn,
  262.     WhitePawn, WhitePawn, WhitePawn, WhitePawn },
  263.     { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
  264.     EmptySquare, EmptySquare, EmptySquare, EmptySquare },
  265.     { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
  266.     EmptySquare, EmptySquare, EmptySquare, EmptySquare },
  267.     { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
  268.     EmptySquare, EmptySquare, EmptySquare, EmptySquare },
  269.     { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
  270.     EmptySquare, EmptySquare, EmptySquare, EmptySquare },
  271.     { BlackPawn, BlackPawn, BlackPawn, BlackPawn,
  272.     BlackPawn, BlackPawn, BlackPawn, BlackPawn },
  273.     { BlackRook, BlackKnight, BlackBishop, BlackQueen,
  274.     BlackKing, BlackBishop, BlackKnight, BlackRook }
  275. };
  276.  
  277.  
  278. void InitBackEnd1()
  279. {
  280.     int matched, min, sec;
  281.  
  282.     /*
  283.      * Initialize game list
  284.      */
  285.     ListNew(&gameList);
  286.  
  287.  
  288.     /*
  289.      * Internet chess server status
  290.      */
  291.     if (appData.icsActive) {
  292.     appData.matchMode = FALSE;
  293. #ifdef ZIPPY    
  294.     appData.noChessProgram = !appData.zippyPlay;
  295. #else
  296.     appData.zippyPlay = FALSE;
  297.     appData.zippyTalk = FALSE;
  298.     appData.noChessProgram = TRUE;
  299. #endif
  300.     } else {
  301.     appData.zippyTalk = appData.zippyPlay = FALSE;
  302.     }
  303.  
  304.     /*
  305.      * Parse timeControl resource
  306.      */
  307.     if (!ParseTimeControl(appData.timeControl)) {
  308.     /* Can't use DisplayFatalError yet; need more initialization */
  309.     fprintf(stderr, "Bad timeControl option %s", appData.timeControl);
  310.     exit(2);
  311.     }
  312.     if (appData.icsActive) timeControl = 0;
  313.     
  314.     /*
  315.      * Parse searchTime resource
  316.      */
  317.     if (*appData.searchTime != NULLCHAR) {
  318.     matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
  319.     if (matched == 1) {
  320.         searchTime = min * 60;
  321.     } else if (matched == 2) {
  322.         searchTime = min * 60 + sec;
  323.     } else {
  324.         /* Can't use DisplayFatalError yet; need more initialization */
  325.         fprintf(stderr, "Bad searchTime option %s", appData.searchTime);
  326.         exit(2);
  327.     }
  328.     }
  329.     
  330.     if (appData.icsActive) {
  331.     appData.clockMode = TRUE;
  332.     } else if ((*appData.searchTime != NULLCHAR) ||
  333.            (appData.searchDepth > 0) ||
  334.            appData.noChessProgram) {
  335.     appData.clockMode = FALSE;
  336.     firstSendTime = secondSendTime = 0;
  337.     }
  338.  
  339. #ifdef ZIPPY
  340.     ZippyInit();
  341. #endif
  342. }
  343.  
  344. int ParseTimeControl(tc)
  345.      char *tc;
  346. {
  347.     int matched, min, sec;
  348.  
  349.     matched = sscanf(tc, "%d:%d", &min, &sec);
  350.     if (matched == 1) {
  351.     timeControl = min * 60 * 1000;
  352.     } else if (matched == 2) {
  353.     timeControl = (min * 60 + sec) * 1000;
  354.     } else {
  355.     return FALSE;
  356.     }
  357.     return TRUE;
  358. }
  359.  
  360. void InitBackEnd2()
  361. {
  362.     char buf[MSG_SIZ];
  363.     int err;
  364.  
  365.     if (appData.icsActive) {
  366.     err = establish();
  367.     if (err != 0) {
  368.         if (*appData.icsCommPort != NULLCHAR) {
  369.         sprintf(buf, "Could not open comm port %s",  
  370.             appData.icsCommPort);
  371.         } else {
  372.         sprintf(buf, "Could not connect to host %s, port %s",  
  373.             appData.icsHost, appData.icsPort);
  374.         }
  375.         DisplayFatalError(buf, err, 1);
  376.         return;
  377.     }
  378.     SetICSMode();
  379.     telnetISR = AddInputSource(icsPR, FALSE, read_from_ics);
  380.     fromUserISR = AddInputSource(NoProc, FALSE, read_from_player);
  381.     } else if (appData.noChessProgram) {
  382.     SetNCPMode();
  383.     } else {
  384.     SetGNUMode();
  385.     }
  386.  
  387.     if (*appData.cmailGameName != NULLCHAR) {
  388.     SetCmailMode();
  389.     OpenLoopback(&cmailPR);
  390.     cmailISR = AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack);
  391.     }
  392.     
  393.     if (appData.matchMode) {
  394.     /* Set up machine vs. machine match */
  395.     if (appData.noChessProgram) {
  396.         DisplayFatalError("Can't have a match with no chess programs",
  397.                   0, 2);
  398.         return;
  399.     }
  400.     Reset(TRUE);
  401.     matchMode = TRUE;
  402.     if (*appData.loadGameFile != NULLCHAR) {
  403.         if (!LoadGameFromFile(appData.loadGameFile,
  404.                   appData.loadGameIndex,
  405.                   appData.loadGameFile, FALSE)) {
  406.         DisplayFatalError("Bad game file", 0, 1);
  407.         return;
  408.         }
  409.     } else if (*appData.loadPositionFile != NULLCHAR) {
  410.         if (!LoadPositionFromFile(appData.loadPositionFile,
  411.                       appData.loadPositionIndex,
  412.                       appData.loadPositionFile)) {
  413.         DisplayFatalError("Bad position file", 0, 1);
  414.         return;
  415.         }
  416.     }
  417.     TwoMachinesEvent();
  418.     } else if (*appData.cmailGameName != NULLCHAR) {
  419.     /* Set up cmail mode */
  420.     ReloadCmailMsgEvent(TRUE);
  421.     } else {
  422.     /* Set up other modes */
  423.     Reset(TRUE);
  424.     if (*appData.loadGameFile != NULLCHAR) {
  425.         (void) LoadGameFromFile(appData.loadGameFile,
  426.                     appData.loadGameIndex,
  427.                     appData.loadGameFile, TRUE);
  428.     } else if (*appData.loadPositionFile != NULLCHAR) {
  429.         (void) LoadPositionFromFile(appData.loadPositionFile,
  430.                     appData.loadPositionIndex,
  431.                     appData.loadPositionFile);
  432.     }
  433.     }
  434. }
  435.  
  436. /*
  437.  * Establish will establish a contact to a remote host.port.
  438.  * Sets icsPR to a ProcRef for a process (or pseudo-process)
  439.  *  used to talk to the host.
  440.  * Returns 0 if okay, error code if not.
  441.  */
  442. int establish()
  443. {
  444.     char buf[MSG_SIZ];
  445.  
  446.     if (*appData.icsCommPort != NULLCHAR) {
  447.     /* Talk to the host through a serial comm port */
  448.     return OpenCommPort(appData.icsCommPort, &icsPR);
  449.  
  450.     } else if (*appData.gateway != NULLCHAR) {
  451.     if (*appData.remoteShell == NULLCHAR) {
  452.         /* Use the rcmd protocol to run telnet program on a gateway host */
  453.         sprintf(buf, "%s %s %s",
  454.             appData.telnetProgram, appData.icsHost, appData.icsPort);
  455.         return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
  456.  
  457.     } else {
  458.         /* Use the rsh program to run telnet program on a gateway host */
  459.         if (*appData.remoteUser == NULLCHAR) {
  460.         sprintf(buf, "%s %s %s %s %s", appData.remoteShell,
  461.             appData.gateway, appData.telnetProgram,
  462.             appData.icsHost, appData.icsPort);
  463.         } else {
  464.         sprintf(buf, "%s -l %s %s %s %s %s",
  465.             appData.remoteShell, appData.remoteUser,
  466.             appData.gateway, appData.telnetProgram,
  467.             appData.icsHost, appData.icsPort);
  468.         }
  469.         return StartChildProcess(buf, &icsPR);
  470.  
  471.     }
  472.     } else if (appData.useTelnet) {
  473.     return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
  474.  
  475.     } else {
  476.     /* TCP socket interface differs somewhat between
  477.        Unix and NT; handle details in the front end.
  478.     */
  479.     return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
  480.     }
  481. }
  482.  
  483. void show_bytes(fp, buf, count)
  484.      FILE *fp;
  485.      char *buf;
  486.      int count;
  487. {
  488.     while (count--) {
  489.       if (*buf < 040 || *(unsigned char *) buf > 0177) {
  490.         fprintf(fp, "\\%03o", *buf & 0xff);
  491.     } else {
  492.         putc(*buf, fp);
  493.     }
  494.     buf++;
  495.     }
  496.     fflush(fp);
  497. }
  498.  
  499. void read_from_player(isr, message, count, error)
  500.      InputSourceRef isr;
  501.      char *message;
  502.      int count;
  503.      int error;
  504. {
  505.     int outError, outCount;
  506.     static int eofCount = 0;
  507.  
  508.     if (count > 0) {
  509.     if (appData.debugMode) {
  510.         fprintf(debugFP, "Sending to ICS: ");
  511.         show_bytes(debugFP, message, count);
  512.     }
  513.     outCount = OutputToProcess(icsPR, message, count, &outError);
  514.     eofCount = 0;
  515.     if (outCount < count) {
  516.         RemoveInputSource(isr);
  517.         DisplayFatalError("Error writing to ICS", outError, 1);
  518.     }
  519.     } else if (count < 0) {
  520.     RemoveInputSource(isr);
  521.     DisplayFatalError("Error reading from keyboard", error, 1);
  522.     } else if (eofCount++ > 0) {
  523.     RemoveInputSource(isr);
  524.     DisplayFatalError("Got end of file from keyboard", 0, 0);
  525.     }
  526. }
  527.  
  528. void SendToICS(s)
  529.      char *s;
  530. {
  531.     int count, outCount, outError;
  532.  
  533.     if (icsPR == NULL) return;
  534.  
  535.     count = strlen(s);
  536.     if (appData.debugMode) {
  537.     fprintf(debugFP, "Sending to ICS: ");
  538.     show_bytes(debugFP, s, count);
  539.     }
  540.     outCount = OutputToProcess(icsPR, s, count, &outError);
  541.     if (outCount < count) {
  542.     DisplayFatalError("Error writing to ICS", outError, 1);
  543.     }
  544. }
  545.  
  546.  
  547. static int leftover_start = 0, leftover_len = 0;
  548. char star_match[STAR_MATCH_N][MSG_SIZ];
  549.  
  550. /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
  551.    advance *index beyond it, and set leftover_start to the new value of
  552.    *index; else return FALSE.  If pattern contains the character '*', it
  553.    matches any sequence of characters not containing '\r', '\n', or the
  554.    character following the '*' (if any), and the matched sequence(s) are
  555.    copied into star_match.
  556. */
  557. int looking_at(buf, index, pattern)
  558.      char *buf;
  559.      int *index;
  560.      char *pattern;
  561. {
  562.     char *bufp = &buf[*index], *patternp = pattern;
  563.     int star_count = 0;
  564.     char *matchp = star_match[0];
  565.     
  566.     for (;;) {
  567.     if (*patternp == NULLCHAR) {
  568.         *index = leftover_start = bufp - buf;
  569.         *matchp = NULLCHAR;
  570.         return TRUE;
  571.     }
  572.     if (*bufp == NULLCHAR) return FALSE;
  573.     if (*patternp == '*') {
  574.         if (*bufp == *(patternp + 1)) {
  575.         *matchp = NULLCHAR;
  576.         matchp = star_match[++star_count];
  577.         patternp += 2;
  578.         bufp++;
  579.         continue;
  580.         } else if (*bufp == '\n' || *bufp == '\r') {
  581.         patternp++;
  582.         if (*patternp == NULLCHAR)
  583.           continue;
  584.         else
  585.           return FALSE;
  586.         } else {
  587.         *matchp++ = *bufp++;
  588.         continue;
  589.         }
  590.     }
  591.     if (*patternp != *bufp) return FALSE;
  592.     patternp++;
  593.     bufp++;
  594.     }
  595. }
  596.  
  597. /*-- Game start info cache: --*/
  598. int gs_gamenum;
  599. char gs_kind[MSG_SIZ];
  600. /*----------------------------*/
  601.  
  602. void read_from_ics(isr, data, count, error)
  603.      InputSourceRef isr;
  604.      char *data;
  605.      int count;
  606.      int error;
  607. {
  608. #define BUF_SIZE 8192
  609. #define STARTED_NONE 0
  610. #define STARTED_MOVES 1
  611. #define STARTED_BOARD 2
  612. #define STARTED_OBSERVE 3
  613.     
  614.     static int started = STARTED_NONE;
  615.     static char parse[20000];
  616.     static int parse_pos;
  617.     static char buf[BUF_SIZE + 1];
  618.     static int firstTime = TRUE;
  619.     static int savingComment = FALSE;
  620.     
  621.     char str[500];
  622.     int i, oldi;
  623.     int buf_len;
  624.     int next_out;
  625.     
  626.     if (count > 0) {
  627.     /* If last read ended with a partial line that we couldn't parse,
  628.        prepend it to the new read and try again. */
  629.     if (leftover_len > 0) {
  630.         for (i=0; i<leftover_len; i++)
  631.           buf[i] = buf[leftover_start + i];
  632.     }
  633.  
  634.     /* Copy in new characters, removing nulls and \r's */
  635.     buf_len = leftover_len;
  636.     for (i = 0; i < count; i++) {
  637.         if (data[i] != NULLCHAR && data[i] != '\r')
  638.           buf[buf_len++] = data[i];
  639.     }
  640.  
  641.     buf[buf_len] = NULLCHAR;
  642.     next_out = leftover_len;
  643.     leftover_start = 0;
  644.     
  645.     i = 0;
  646.     while (i < buf_len) {
  647.         
  648. #define TN_WILL 0373
  649. #define TN_WONT 0374
  650. #define TN_DO   0375
  651. #define TN_DONT 0376
  652. #define TN_IAC  0377
  653. #define TN_ECHO 0001
  654. #define TN_SGA  0003
  655. #define TN_PORT 23
  656.  
  657.         /* Deal with part of the TELNET option negotiation
  658.            protocol.  We refuse to do anything beyond the
  659.            defaults, except that we allow the WILL ECHO option,
  660.            which ICS uses to turn off password echoing when we are
  661.            directly connected to it.  We must reject this option
  662.            when we are talking to a real telnet server (port 23),
  663.            because most telnet servers want to go into
  664.            character-by-character mode and handle all the
  665.            echoing. */
  666.         if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
  667.         static int remoteEchoOption = FALSE;  /* telnet ECHO option */
  668.         unsigned char reply[4];
  669.         oldi = i;
  670.         reply[0] = TN_IAC;
  671.         reply[3] = NULLCHAR;
  672.         switch ((unsigned char) buf[++i]) {
  673.           case TN_WILL:
  674.             if (appData.debugMode)
  675.               fprintf(debugFP, "\n<WILL ");
  676.             switch (reply[2] = (unsigned char) buf[++i]) {
  677.               case TN_ECHO:
  678.             if (appData.debugMode)
  679.               fprintf(debugFP, "ECHO ");
  680.             /* Reply only if this is a change, according
  681.                to the protocol rules. */
  682.             if (remoteEchoOption) break;
  683.             if (atoi(appData.icsPort) == TN_PORT) {
  684.                 reply[1] = TN_DONT;
  685.                 if (appData.debugMode)
  686.                   fprintf(debugFP, ">DONT ECHO ");
  687.             } else {
  688.                 EchoOff();
  689.                 reply[1] = TN_DO;
  690.                 if (appData.debugMode)
  691.                   fprintf(debugFP, ">DO ECHO ");
  692.                 remoteEchoOption = TRUE;
  693.             }
  694.             SendToICS((char *) reply);
  695.             break;
  696.               default:
  697.             if (appData.debugMode)
  698.               fprintf(debugFP, "%d ", reply[2]);
  699.             /* Whatever this is, we don't want it. */
  700.             reply[1] = TN_DONT;
  701.             if (appData.debugMode)
  702.               fprintf(debugFP, ">DONT %d ", reply[2]);
  703.             SendToICS((char *) reply);
  704.             break;
  705.             }
  706.             break;
  707.           case TN_WONT:
  708.             if (appData.debugMode)
  709.               fprintf(debugFP, "\n<WONT ");
  710.             switch (reply[2] = (unsigned char) buf[++i]) {
  711.               case TN_ECHO:
  712.             if (appData.debugMode)
  713.               fprintf(debugFP, "ECHO ");
  714.             /* Reply only if this is a change, according
  715.                to the protocol rules. */
  716.             if (!remoteEchoOption) break;
  717.             EchoOn();
  718.             reply[1] = TN_DONT;
  719.             if (appData.debugMode)
  720.               fprintf(debugFP, ">DONT ECHO ");
  721.             remoteEchoOption = FALSE;
  722.             SendToICS((char *) reply);
  723.             break;
  724.               default:
  725.             if (appData.debugMode)
  726.               fprintf(debugFP, "%d ", (unsigned char) buf[i]);
  727.             /* Whatever this is, it must already be turned
  728.                off, because we never agree to turn on
  729.                anything non-default, so according to the
  730.                protocol rules, we don't reply. */
  731.             break;
  732.             }
  733.             break;
  734.           case TN_DO:
  735.             if (appData.debugMode)
  736.               fprintf(debugFP, "\n<DO ");
  737.             switch (reply[2] = (unsigned char) buf[++i]) {
  738.               default:
  739.             /* Whatever this is, we refuse to do it. */
  740.             reply[1] = TN_WONT;
  741.             if (appData.debugMode)
  742.               fprintf(debugFP, "%d >WONT %d ", reply[2], reply[2]);
  743.             SendToICS((char *) reply);
  744.             break;
  745.             }
  746.             break;
  747.           case TN_DONT:
  748.             if (appData.debugMode)
  749.               fprintf(debugFP, "\n<DONT ");
  750.             switch (reply[2] = (unsigned char) buf[++i]) {
  751.               default:
  752.             if (appData.debugMode)
  753.               fprintf(debugFP, "%d ", reply[2]);
  754.             /* Whatever this is, we are already not doing
  755.                it, because we never agree to do anything
  756.                non-default, so according to the protocol
  757.                rules, we don't reply. */
  758.             break;
  759.             }
  760.             break;
  761.           case TN_IAC:
  762.             if (appData.debugMode)
  763.               fprintf(debugFP, "\n<IAC ");
  764.             /* Doubled IAC; pass it through */
  765.             i--;
  766.             break;
  767.           default:
  768.             if (appData.debugMode)
  769.               fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
  770.             /* Drop all other telnet commands on the floor */
  771.             break;
  772.         }
  773.         if (oldi > next_out)
  774.           fwrite(&buf[next_out], oldi - next_out, 1, toUserFP);
  775.         if (++i > next_out)
  776.           next_out = i;
  777.         continue;
  778.         }
  779.         
  780.             /* Kludge to deal with rcmd protocol */
  781.         if (firstTime && looking_at(buf, &i, "\001*")) {
  782.         DisplayFatalError(&buf[1], 0, 1);
  783.         continue;
  784.         } else {
  785.             firstTime = FALSE;
  786.         }
  787.  
  788. #ifdef ZIPPY
  789.         if (appData.zippyTalk || appData.zippyPlay) {
  790.         if (ZippyControl(buf, &i)) continue;
  791.         if (appData.zippyTalk && ZippyConverse(buf, &i)) continue;
  792.         if (appData.zippyPlay && ZippyMatch(buf, &i)) continue;
  793.         } 
  794. #endif
  795.         oldi = i;
  796.         if (looking_at(buf, &i, "* shouts: *") ||
  797.             looking_at(buf, &i, "* s-shouts: *") ||
  798.             looking_at(buf, &i, "* c-shouts: *") ||
  799.         looking_at(buf, &i, "--> * *")) {
  800.         /* Ignore shouts */
  801.         continue;
  802.         }
  803.         if (looking_at(buf, &i, "* tells you: *") ||
  804.         looking_at(buf, &i, "* says: *") ||
  805.         looking_at(buf, &i, "* whispers: *") ||
  806.         looking_at(buf, &i, "* kibitzes: *") ||
  807.         looking_at(buf, &i, "\\   *") ||
  808.         looking_at(buf, &i, "*(*): *")) {
  809.  
  810.         if (started == STARTED_NONE && appData.autoComment &&
  811.             (gameMode == IcsObserving || gameMode == IcsPlayingWhite ||
  812.              gameMode == IcsPlayingBlack) &&
  813.             (buf[oldi] != '\\' || savingComment)) {
  814.             char *player = &buf[oldi];
  815.             char *pend = player;
  816.             int len1 = 0, len2 = 0;
  817.             if (*player == '\033') {
  818.             /* Strip off highlighting escape sequences */
  819.             while (!isalpha(*player)) player++;
  820.             player++;
  821.             pend = strchr(player, '\033');
  822.             if (pend != NULL) {
  823.                 len1 = pend - player;
  824.                 strncpy(parse, player, len1);
  825.                 while (!isalpha(*pend)) pend++;
  826.                 pend++;
  827.             } else {
  828.                 pend = player;
  829.             }
  830.             }
  831.             len2 = i - oldi - (pend - &buf[oldi]);
  832.             strncpy(parse + len1, pend, len2);
  833.             parse[len1 + len2] = NULLCHAR;
  834.             AppendComment(forwardMostMove, parse);
  835.             savingComment = TRUE;
  836.         } else {
  837.             savingComment = FALSE;
  838.         }
  839.         continue;
  840.         }
  841.  
  842.         if (looking_at(buf, &i, "Black Strength :") ||
  843.         looking_at(buf, &i, "<<< style 10 board >>>") ||
  844.         looking_at(buf, &i, "<10>") ||
  845.         looking_at(buf, &i, "#@#")) {
  846.         /* Wrong board style */
  847.         SendToICS("set style 12\n");
  848.                 SendToICS("refresh\n");
  849.         continue;
  850.         }
  851.         
  852.         oldi = i;
  853.         if (looking_at(buf, &i, "\n<12> ") ||
  854.         looking_at(buf, &i, "<12> ")) {
  855.         if (oldi > next_out) {
  856.             fwrite(&buf[next_out], oldi - next_out, 1, toUserFP);
  857.             fflush(toUserFP);
  858.         }
  859.         next_out = i;
  860.         started = STARTED_BOARD;
  861.         parse_pos = 0;
  862.         continue;
  863.         }
  864.  
  865.         if (looking_at(buf, &i, "* *vs. * *--- *")) {
  866.         /* Header for a move list -- first line */
  867.         /* We might want to save some of these fields but
  868.            for now we don't */
  869.         switch (ics_getting_history) {
  870.           case H_FALSE:
  871.             switch (gameMode) {
  872.               case IcsIdle:
  873.               case BeginningOfGame:
  874.             /* User typed "moves" or "oldmoves" while we
  875.                were idle.  Pretend we asked for these
  876.                moves and soak them up so user can step
  877.                through them and/or save them.
  878.             */
  879.             Reset(FALSE);
  880.             gameMode = IcsObserving;
  881.             ModeHighlight();
  882.             ics_gamenum = -1;
  883.             ics_getting_history = H_GOT_REQ_HEADER;
  884.             break;
  885.               case EditGame: /*?*/
  886.               case EditPosition: /*?*/
  887.             /* Should above feature work in these modes too? */
  888.             /* For now it doesn't */
  889.             ics_getting_history = H_GOT_UNREQ_HEADER;
  890.             break;
  891.               default:
  892.             ics_getting_history = H_GOT_UNREQ_HEADER;
  893.             break;
  894.             }
  895.             break;
  896.           case H_REQUESTED:
  897.             /* All is well */
  898.             ics_getting_history = H_GOT_REQ_HEADER;
  899.             break;
  900.           case H_GOT_REQ_HEADER:
  901.           case H_GOT_UNREQ_HEADER:
  902.           case H_GETTING_MOVES:
  903.             /* Should not happen */
  904.             DisplayError("Error gathering move list: two headers", 0);
  905.             ics_getting_history = H_FALSE;
  906.             break;
  907.         }
  908.         continue;
  909.         }
  910.  
  911.         if (looking_at(buf, &i,
  912.               "* * match, initial time: * minutes, increment: * seconds.")) {
  913.         /* Header for a move list -- second line */
  914.         /* Initial board will follow if this is a wild game */
  915.         /* Again, we might want to save some of these fields later */
  916.         /* For now we do nothing with them. */
  917.         continue;
  918.         }
  919.  
  920.         oldi = i;
  921.         if (looking_at(buf, &i, "Move  ")) {
  922.         /* Beginning of a move list */
  923.         switch (ics_getting_history) {
  924.           case H_FALSE:
  925.             /* Normally should not happen */
  926.             /* Maybe user hit reset while we were parsing */
  927.             break;
  928.           case H_REQUESTED:
  929.           case H_GETTING_MOVES:
  930.             /* Should not happen */
  931.             DisplayError("Error gathering move list: no header", 0);
  932.             ics_getting_history = H_FALSE;
  933.             break;
  934.           case H_GOT_REQ_HEADER:
  935.             ics_getting_history = H_GETTING_MOVES;
  936.             started = STARTED_MOVES;
  937.             parse_pos = 0;
  938.             if (oldi > next_out) {
  939.             fwrite(&buf[next_out], oldi - next_out, 1, toUserFP);
  940.             fflush(toUserFP);
  941.             }
  942.             break;
  943.           case H_GOT_UNREQ_HEADER:
  944.             ics_getting_history = H_FALSE;
  945.             break;
  946.         }
  947.         continue;
  948.         }                
  949.         
  950.         if (looking_at(buf, &i, "% ") ||
  951.         (started == STARTED_MOVES && looking_at(buf, &i, ": "))) {
  952.         savingComment = FALSE;
  953.         switch (started) {
  954.           case STARTED_MOVES:
  955.             started = STARTED_NONE;
  956.             parse[parse_pos] = NULLCHAR;
  957.             ParseGameHistory(parse);
  958.              if (gameMode == IcsObserving && ics_gamenum == -1) {
  959.             /* Moves came from oldmoves or moves command
  960.                while we weren't doing anything else.
  961.             */
  962.             currentMove = forwardMostMove;
  963.             flipView = appData.flipView;
  964.             DrawPosition(FALSE, boards[currentMove]);
  965.             DisplayBothClocks();
  966.             sprintf(str, "%s vs. %s",
  967.                 gameInfo.white, gameInfo.black);
  968.             DisplayTitle(str);
  969.             gameMode = IcsIdle;
  970.             } else {
  971.             /* Moves were history of an active game */
  972.             if (gameInfo.resultDetails != NULL) {
  973.                 free(gameInfo.resultDetails);
  974.                 gameInfo.resultDetails = NULL;
  975.             }
  976.             }
  977.             DisplayMove(currentMove - 1);
  978. #ifdef NOTDEF
  979.             SendToICS("\n");  /*kludge: force a prompt*/
  980. #endif
  981.             next_out = i;
  982.             ics_getting_history = H_FALSE;
  983.             break;
  984.  
  985.           case STARTED_OBSERVE:
  986.             started = STARTED_NONE;
  987.             SendToICS("refresh\n");
  988.             break;
  989.  
  990.           default:
  991.             break;
  992.         }
  993.         continue;
  994.         }
  995.         
  996.         if ((started == STARTED_MOVES || started == STARTED_BOARD) &&
  997.         i >= leftover_len) {
  998.         /* Accumulate characters in move list or board */
  999.         parse[parse_pos++] = buf[i];
  1000.         }
  1001.         
  1002.         /* Start of game messages.  Mostly we detect start of game
  1003.            when the first board image arrives.  On some versions
  1004.            of the ICS, though, we need to do a "refresh" after starting
  1005.            to observe in order to get the current board right away. */
  1006.         if (looking_at(buf, &i, "Adding game * to observation list")) {
  1007.         started = STARTED_OBSERVE;
  1008.         continue;
  1009.         }
  1010.  
  1011.         /* Handle auto-observe */
  1012.         if (appData.autoObserve &&
  1013.         (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
  1014.         looking_at(buf, &i, "Game notification: * ")) {
  1015.         char *player = star_match[0];
  1016.         char *pend;
  1017.         if (*player == '\033') {
  1018.             /* Strip off highlighting escape sequences */
  1019.             while (!isalpha(*player)) player++;
  1020.             player++;
  1021.             pend = strchr(player, '\033');
  1022.             if (pend != NULL) *pend = NULLCHAR;
  1023.         }
  1024.         sprintf(str, "observe %s\n", player);
  1025.         SendToICS(str);
  1026.         continue;
  1027.         }
  1028.  
  1029.         /* Deal with automatic examine mode after a game,
  1030.            and with IcsObserving -> IcsExamining transition */
  1031.         if (looking_at(buf, &i, "Entering examine mode for game *.") ||
  1032.         looking_at(buf, &i, "has made you an examiner of game *.")) {
  1033.  
  1034.         int gamenum = atoi(star_match[0]);
  1035.         if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
  1036.             gamenum == ics_gamenum) {
  1037.             /* We were already playing or observing this game;
  1038.                no need to refetch history */
  1039.             gameMode = IcsExamining;
  1040.             if (pausing) {
  1041.             pauseExamForwardMostMove = forwardMostMove;
  1042.             } else if (currentMove < forwardMostMove) {
  1043.             ForwardInner(forwardMostMove);
  1044.             }
  1045.         } else {
  1046.             /* I don't think this case really can happen */
  1047.             SendToICS("refresh\n");
  1048.         }
  1049.         continue;
  1050.         }    
  1051.         
  1052.         /* Error messages */
  1053.         if (ics_user_moved) {
  1054.         if (looking_at(buf, &i, "Illegal move") ||
  1055.             looking_at(buf, &i, "Not a legal move") ||
  1056.             looking_at(buf, &i, "Your king is in check") ||
  1057.             looking_at(buf, &i, "It isn't your turn")) {
  1058.             /* Illegal move */
  1059.             ics_user_moved = 0;
  1060.             if (forwardMostMove > backwardMostMove) {
  1061.             currentMove = --forwardMostMove;
  1062.             DisplayError("Illegal move\n(rejected by ICS)", 0);
  1063.             DrawPosition(FALSE, boards[currentMove]);
  1064.             SwitchClocks();
  1065.             }
  1066.             continue;
  1067.         }
  1068.         }
  1069.  
  1070.         if (looking_at(buf, &i, "still have time") ||
  1071.         looking_at(buf, &i, "not out of time")) {
  1072.         /* We must have called his flag a little too soon */
  1073.         whiteFlag = blackFlag = FALSE;
  1074.         continue;
  1075.         }
  1076.  
  1077.         if (looking_at(buf, &i, "added * seconds to") ||
  1078.         looking_at(buf, &i, "seconds were added to")) {
  1079.         /* Update the clocks */
  1080.         SendToICS("refresh\n");
  1081.         continue;
  1082.         }
  1083.  
  1084.         if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
  1085.         ics_clock_paused = TRUE;
  1086.         StopClocks();
  1087.         continue;
  1088.         }
  1089.  
  1090.         if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
  1091.         ics_clock_paused = FALSE;
  1092.         StartClocks();
  1093.         continue;
  1094.         }
  1095.  
  1096.         /* Improved generic end-of-game messages */
  1097.         if (looking_at(buf, &i, "{Game * (* vs. *) *} *")) {
  1098.         /* star_match[0] is the game number */
  1099.         /*           [1] is the white player's name */
  1100.         /*           [2] is the black player's name */
  1101.         /*           [3] is the reason for the game end */
  1102.         /*           [4] is a PGN end game-token */
  1103.         int gamenum = atoi(star_match[0]);
  1104.         char *why = star_match[3];
  1105.         char *endtoken = star_match[4];
  1106.         ChessMove endtype = (ChessMove) 0;
  1107.         /*  Suppress warnings on uninitialized variables    */
  1108.  
  1109.         if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
  1110.             ics_gamenum != gamenum) {
  1111.             continue;
  1112.         }
  1113.         switch (endtoken[0]) {
  1114.           case '*':
  1115.             endtype = GameUnfinished;
  1116.             break;
  1117.           case '0':
  1118.             endtype = BlackWins;
  1119.             break;
  1120.           case '1':
  1121.             if (endtoken[1] == '/')
  1122.               endtype = GameIsDrawn;
  1123.             else
  1124.               endtype = WhiteWins;
  1125.             break;
  1126.         }
  1127.         GameEnds(endtype, why, GE_ICS);
  1128.         continue;
  1129.         }
  1130.  
  1131.         /* Start/end-of-game messages */
  1132.         if (looking_at(buf, &i, "{Game * (* vs. *)* * *}")) {
  1133.         /* Generic game start/end messages */
  1134.         /* star_match[0] is the game number */
  1135.         /*           [1] is the white player's name */
  1136.         /*           [2] is the black player's name */
  1137.         /*           [3] is either ":" or empty (don't care) */
  1138.         /*           [4] is usually the loser's name or a noise word */
  1139.         /*           [5] contains the reason for the game end */
  1140.         int gamenum = atoi(star_match[0]);
  1141.         char *white = star_match[1];
  1142.         char *loser = star_match[4];
  1143.         char *why = star_match[5];
  1144.         
  1145.         /* Game start messages */
  1146.         if (strcmp(loser, "Creating") == 0 ||
  1147.             strcmp(loser, "Continuing") == 0) {
  1148.             gs_gamenum = gamenum;
  1149.             strcpy(gs_kind, why);  /* why = type of game */
  1150. #ifdef ZIPPY
  1151.             ZippyGameStart(white, star_match[2]);
  1152. #endif /*ZIPPY*/
  1153.             continue;
  1154.         }
  1155.  
  1156.         /* Game end messages */
  1157.         if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
  1158.             ics_gamenum != gamenum) {
  1159.             continue;
  1160.         }
  1161.  
  1162.         if (StrStr(why, "checkmate")) {
  1163.             if (strcmp(loser, white) == 0)
  1164.               GameEnds(BlackWins, "Black mates", GE_ICS);
  1165.             else
  1166.               GameEnds(WhiteWins, "White mates", GE_ICS);
  1167.         } else if (StrStr(why, "resign")) {
  1168.             if (strcmp(loser, white) == 0)
  1169.               GameEnds(BlackWins, "White resigns", GE_ICS);
  1170.             else
  1171.               GameEnds(WhiteWins, "Black resigns", GE_ICS);
  1172.         } else if (StrStr(why, "forfeits on time")) {
  1173.             if (strcmp(loser, white) == 0)
  1174.               GameEnds(BlackWins, "Black wins on time", GE_ICS);
  1175.             else
  1176.               GameEnds(WhiteWins, "White wins on time", GE_ICS);
  1177.         } else if (StrStr(why, "stalemate")) {
  1178.             GameEnds(GameIsDrawn, "Stalemate", GE_ICS);
  1179.         } else if (StrStr(why, "drawn by mutual agreement")) {
  1180.             GameEnds(GameIsDrawn, "Draw agreed", GE_ICS);
  1181.         } else if (StrStr(why, "repetition")) {
  1182.             GameEnds(GameIsDrawn, "Draw by repetition", GE_ICS);
  1183.         } else if (StrStr(why, "50")) {
  1184.             GameEnds(GameIsDrawn, "50 move rule", GE_ICS);
  1185.         } else if (StrStr(why, "neither player has mating")) {
  1186.             GameEnds(GameIsDrawn, "Insufficient material", GE_ICS);
  1187.         } else if (StrStr(why, "no material")) {
  1188.             GameEnds(GameIsDrawn,
  1189.                  "Insufficient material to win on time", GE_ICS);
  1190.         } else if (StrStr(why, "time")) {
  1191.             GameEnds(GameIsDrawn, "Both players ran out of time",
  1192.                  GE_ICS);
  1193.         } else if (StrStr(why, "disconnected and forfeits")) {
  1194.             /* in this case the word "abuser" preceded the loser */
  1195.             loser = why;
  1196.             why = strchr(loser, ' ');
  1197.             *why++ = NULLCHAR;
  1198.             if (strcmp(loser, white) == 0)
  1199.               GameEnds(BlackWins, "Forfeit", GE_ICS);
  1200.             else
  1201.               GameEnds(WhiteWins, "Forfeit", GE_ICS);
  1202.         } else if (StrStr(why, "assert")) {
  1203.             /* "loser" is actually the winner in this case */
  1204.             if (strcmp(loser, white) == 0)
  1205.               GameEnds(WhiteWins, "White asserts a win", GE_ICS);
  1206.             else
  1207.               GameEnds(BlackWins, "Black asserts a win", GE_ICS);
  1208.         } else if (StrStr(why, "aborted")) {
  1209.             GameEnds(GameUnfinished, "Game aborted", GE_ICS);
  1210.         } else if (StrStr(why, "removed")) {
  1211.             GameEnds(GameUnfinished, "Game aborted", GE_ICS);
  1212.         } else if (StrStr(why, "adjourn")) {
  1213.             GameEnds(GameUnfinished, "Game adjourned", GE_ICS);
  1214.         } else {
  1215.             /* Unrecognized game end string */
  1216.             GameEnds(GameUnfinished, why, GE_ICS);
  1217.         }
  1218.         continue;
  1219.         }
  1220.  
  1221.         if (looking_at(buf, &i, "Removing game * from observation") ||
  1222.         looking_at(buf, &i, "Game * (*) has no examiners")) {
  1223.         if (gameMode == IcsObserving &&
  1224.             atoi(star_match[0]) == ics_gamenum)
  1225.           {
  1226.               StopClocks();
  1227.               gameMode = IcsIdle;
  1228.               ics_gamenum = -1;
  1229.               ics_user_moved = FALSE;
  1230.           }
  1231.         continue;
  1232.         }
  1233.  
  1234.         if (looking_at(buf, &i, "You are no longer examining game *.")) {
  1235.         if (gameMode == IcsExamining &&
  1236.             atoi(star_match[0]) == ics_gamenum)
  1237.           {
  1238.               gameMode = IcsIdle;
  1239.               ics_gamenum = -1;
  1240.               ics_user_moved = FALSE;
  1241.           }
  1242.         continue;
  1243.         }
  1244.  
  1245.         /* Advance leftover_start past any newlines we find,
  1246.            so only partial lines can get reparsed */
  1247.         if (looking_at(buf, &i, "\n")) {
  1248.         if (started == STARTED_BOARD) {
  1249.             started = STARTED_NONE;
  1250.             parse[parse_pos] = NULLCHAR;
  1251.             ParseBoard12(parse);
  1252.             ics_user_moved = 0;
  1253.             /* Usually suppress following prompt */
  1254.             if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
  1255.             if (looking_at(buf, &i, "*% ")) {
  1256.                 savingComment = FALSE;
  1257.             }
  1258.             }
  1259.             next_out = i;
  1260.         }
  1261.         continue;
  1262.         }
  1263.  
  1264.         i++;    /* skip unparsed character and loop back */
  1265.     }
  1266.     
  1267.     if (started != STARTED_MOVES && started != STARTED_BOARD
  1268.         && i > next_out) {
  1269.         fwrite(&buf[next_out], i - next_out, 1, toUserFP);
  1270.         fflush(toUserFP);
  1271.     }
  1272.     
  1273.     leftover_len = buf_len - leftover_start;
  1274.     /* if buffer ends with something we couldn't parse,
  1275.        reparse it after appending the next read */
  1276.     
  1277.     } else if (count == 0) {
  1278.     extern char *programName;
  1279.     RemoveInputSource(isr);
  1280.     /*** User probably typed "quit", so don't do this:
  1281.       DisplayFatalError("Connection closed by ICS", 0, 0);
  1282.     ***/
  1283.     fprintf(stderr, "%s: Connection closed by ICS\n", programName);
  1284.     ExitEvent(0);
  1285.     } else {
  1286.     RemoveInputSource(isr);
  1287.     DisplayFatalError("Error reading from ICS", error, 1);
  1288.     }
  1289. }
  1290.  
  1291.  
  1292. /* Board style 12 looks like this:
  1293.  
  1294. <12> r-b---k- pp----pp ---bP--- ---p---- q------- ------P- P--Q--BP -----R-K W -1 0 0 0 0 0 0 paf MaxII 0 2 12 21 25 234 174 24 Q/d7-a4 (0:06) Qxa4 0
  1295.  
  1296.  * The "<12> " is stripped before it gets to this routine.
  1297.  * The trailing 0 (flip state) is a recent addition, and FICS doesn't have it.
  1298.  * Additional trailing fields may be added in the future.
  1299.  */
  1300.  
  1301. #define PATTERN "%72c%c%d%d%d%d%d%d%d%s%s%d%d%d%d%d%d%d%d%s%s%s%d"
  1302.  
  1303. #define RELATION_OBSERVING_PLAYED    0
  1304. #define RELATION_OBSERVING_STATIC   -2   /* examined, oldmoves, or smoves */
  1305. #define RELATION_PLAYING_MYMOVE      1
  1306. #define RELATION_PLAYING_NOTMYMOVE  -1
  1307. #define RELATION_EXAMINING           2
  1308. #define RELATION_ISOLATED_BOARD     -3
  1309.  
  1310. void ParseBoard12(string)
  1311.      char *string;
  1312.     GameMode newGameMode;
  1313.     int gamenum, newGame, relation, basetime, increment, ics_flip = 0;
  1314.     int j, k, n, moveNum, white_stren, black_stren, white_time, black_time;
  1315.     int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
  1316.     char to_play, board_chars[72];
  1317.     char move_str[500], str[500], elapsed_time[500];
  1318.     char black[32], white[32];
  1319.     Board board;
  1320.     
  1321.     newGame = FALSE;
  1322.  
  1323.     if (appData.debugMode)
  1324.       fprintf(debugFP, "Parsing board: %s\n", string);
  1325.  
  1326.     move_str[0] = NULLCHAR;
  1327.     elapsed_time[0] = NULLCHAR;
  1328.     n = sscanf(string, PATTERN, board_chars, &to_play, &double_push,
  1329.            &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
  1330.            &gamenum, white, black, &relation, &basetime, &increment,
  1331.            &white_stren, &black_stren, &white_time, &black_time,
  1332.            &moveNum, str, elapsed_time, move_str, &ics_flip);
  1333.  
  1334.     if (n < 22) {
  1335.     sprintf(str, "Failed to parse board string:\n\"%s\"", string);
  1336.     DisplayError(str, 0);
  1337.     return;
  1338.     }
  1339.  
  1340.     /* ICC gives us relation 0, gamenum -1, for some kinds of boards
  1341.        that do not reflect active (clocks running) games.
  1342.     */
  1343.     if (gamenum == -1) relation = RELATION_OBSERVING_STATIC;
  1344.  
  1345.     switch (relation) {
  1346.       case RELATION_OBSERVING_PLAYED:
  1347.       case RELATION_OBSERVING_STATIC:
  1348.     newGameMode = IcsObserving;
  1349.     break;
  1350.       case RELATION_PLAYING_MYMOVE:
  1351.       case RELATION_PLAYING_NOTMYMOVE:
  1352.     newGameMode =
  1353.       ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
  1354.         IcsPlayingWhite : IcsPlayingBlack;
  1355.     break;
  1356.       case RELATION_EXAMINING:
  1357.     newGameMode = IcsExamining;
  1358.     break;
  1359.       case RELATION_ISOLATED_BOARD:
  1360.       default:
  1361.     /* Just display this board.  If user was doing something else,
  1362.        we will forget about it until the next board comes. */ 
  1363.     newGameMode = IcsIdle;
  1364.     break;
  1365.     }
  1366.     
  1367.     /* Convert the move number to internal form */
  1368.     moveNum = (moveNum - 1) * 2;
  1369.     if (to_play == 'B') moveNum++;
  1370.  
  1371.     /* Deal with initial board display on move listing
  1372.        of wild games.
  1373.     */
  1374.     switch (ics_getting_history) {
  1375.       case H_FALSE:
  1376.       case H_REQUESTED:
  1377.     break;
  1378.       case H_GOT_REQ_HEADER:
  1379.     /* This is an initial board that we want */
  1380.     gamenum = ics_gamenum;
  1381.     moveNum = 0; /* !! ICS bug workaround */
  1382.     break;
  1383.       case H_GOT_UNREQ_HEADER:
  1384.     /* This is an initial board that we don't want */
  1385.     return;
  1386.       case H_GETTING_MOVES:
  1387.     /* Should not happen */
  1388.     DisplayError("Error gathering move list: extra board", 0);
  1389.     ics_getting_history = H_FALSE;
  1390.     return;
  1391.     }
  1392.  
  1393.     /* Take action if this is the first board of a new game */
  1394.     if (gamenum != ics_gamenum || newGameMode != gameMode ||
  1395.     relation == RELATION_ISOLATED_BOARD) {
  1396.     /* Check if trying to do two things at once */
  1397.     /* xboard can't handle two games at once */
  1398.     if (gameMode == IcsObserving && newGameMode != IcsIdle) {
  1399.         /* Stop observing the old game */
  1400.         sprintf(str, "observe %d\n", ics_gamenum);
  1401.         SendToICS(str);
  1402.         sprintf(str, "Aren't you observing game %d?\nAttempting to stop observing it.", ics_gamenum);
  1403.         DisplayError(str, 0);
  1404.         /* continue as in normal case */
  1405.     } else if (gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite
  1406.            || gameMode == IcsExamining) {
  1407.         if (newGameMode == IcsObserving) {
  1408.         if (gamenum != -1) {
  1409.             /* Stop observing the new game */
  1410.             SendToICS("observe\n");
  1411.             sprintf(str, "Aren't you %s a game?\nAttempting to stop observing game %d.",
  1412.                 gameMode == IcsExamining ? "examining" : "playing",
  1413.                 gamenum);
  1414.             DisplayError(str, 0);
  1415.         }
  1416.         /* ignore this board */
  1417.         return;
  1418.         }
  1419.         /* else newGameMode ==
  1420.            Ics(PlayingWhite|PlayingBlack|Examining|Idle) 
  1421.                  Maybe we switched sides in the same game, or are
  1422.              playing in a simul.  FICS can do these things.  We
  1423.              just quietly forget about the old game and give our
  1424.              attention to the new one.  Maybe for simuls we should
  1425.              have a mode where we don't fetch the history when
  1426.              switching games in this way.  (I believe the case where
  1427.          newGameMode == IcsExamining is impossible.)
  1428.         */
  1429.     }
  1430.     /* Normal case, or error recovered */
  1431.     Reset(FALSE);
  1432.     newGame = TRUE;
  1433.     if (gamenum == -1) {
  1434.         newGameMode = IcsIdle;
  1435.     } else if (moveNum > 0 && newGameMode != IcsIdle) {
  1436.         /* Need to get game history */
  1437.         ics_getting_history = H_REQUESTED;
  1438.         sprintf(str, "moves %d\n", gamenum);
  1439.         SendToICS(str);
  1440.     }
  1441.  
  1442.     /* Initially flip the board to have black on the bottom if playing
  1443.        black or if the ICS flip flag is set, but let the user change
  1444.        it with the Flip View button. */
  1445.     flipView = (newGameMode == IcsPlayingBlack) || ics_flip;
  1446.  
  1447.     /* Done with values from previous mode; copy in new ones */
  1448.     gameMode = newGameMode;
  1449.     ModeHighlight();
  1450.     ics_gamenum = gamenum;
  1451.     if (gamenum == gs_gamenum) {
  1452.         int klen = strlen(gs_kind);
  1453.         if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
  1454.         sprintf(str, "ICS %s", gs_kind);
  1455.         gameInfo.event = StrSave(str);
  1456.     } else {
  1457.         gameInfo.event = StrSave("ICS game");
  1458.     }
  1459.     gameInfo.site = StrSave(appData.icsHost);
  1460.     gameInfo.date = PGNDate();
  1461.     gameInfo.round = StrSave("-");
  1462.     gameInfo.white = StrSave(white);
  1463.     gameInfo.black = StrSave(black);
  1464.     ics_basetime = basetime * 60 * 1000; /* convert to ms */
  1465.     ics_increment = increment * 1000;    /* convert to ms */
  1466.     if (increment == 0)
  1467.       sprintf(str, "%d", basetime * 60);
  1468.     else
  1469.       sprintf(str, "%d+%d", basetime * 60, increment);
  1470.     gameInfo.timeControl = StrSave(str);
  1471.  
  1472.     /* Silence shouts if requested */
  1473.     if (appData.quietPlay &&
  1474.         (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
  1475.         SendToICS("set shout 0\n");
  1476.     }
  1477.     }
  1478.  
  1479.     /* Throw away game result if anything actually changes in examine mode */
  1480.     if (gameMode == IcsExamining && !newGame) {
  1481.     gameInfo.result = GameUnfinished;
  1482.     if (gameInfo.resultDetails != NULL) {
  1483.         free(gameInfo.resultDetails);
  1484.         gameInfo.resultDetails = NULL;
  1485.     }
  1486.     }
  1487.  
  1488.     /* In pausing && IcsExamining mode, we ignore boards coming
  1489.        in if they are in a different variation than we are. */
  1490.     if (pauseExamInvalid) return;
  1491.     if (pausing && gameMode == IcsExamining) {
  1492.     if (moveNum <= pauseExamForwardMostMove) {
  1493.         pauseExamInvalid = TRUE;
  1494.         forwardMostMove = pauseExamForwardMostMove;
  1495.         return;
  1496.     }
  1497.     }
  1498.  
  1499.     /* Parse the board */
  1500.     for (k = 0; k < 8; k++)
  1501.       for (j = 0; j < 8; j++)
  1502.     board[k][j] = CharToPiece(board_chars[(7-k)*9 + j]);
  1503.     CopyBoard(boards[moveNum], board);
  1504.     if (moveNum == 0) {
  1505.     startedFromSetupPosition =
  1506.       !CompareBoards(board, initialPosition);
  1507.     }
  1508.     
  1509.     if (ics_getting_history == H_GOT_REQ_HEADER) {
  1510.     /* This was an initial position from a move list, not
  1511.        the current position */
  1512.     return;
  1513.     }
  1514.  
  1515.     /* Update currentMove and known move number limits */
  1516.     if (newGame) {
  1517.     forwardMostMove = backwardMostMove = currentMove = moveNum;
  1518.     } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
  1519.            || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
  1520.     forwardMostMove = moveNum;
  1521.     if (!pausing || currentMove > forwardMostMove)
  1522.       currentMove = forwardMostMove;
  1523.     } else {
  1524.     /* New part of history that is not contiguous with old part */ 
  1525.     if (pausing && gameMode == IcsExamining) {
  1526.         pauseExamInvalid = TRUE;
  1527.         forwardMostMove = pauseExamForwardMostMove;
  1528.         return;
  1529.     }
  1530.     forwardMostMove = backwardMostMove = currentMove = moveNum;
  1531.     if (gameMode == IcsExamining && moveNum > 0) {
  1532.         ics_getting_history = H_REQUESTED;
  1533.         sprintf(str, "moves %d\n", gamenum);
  1534.         SendToICS(str);
  1535.     }
  1536.     }
  1537.  
  1538.     /* Update the clocks */
  1539.     timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
  1540.     timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
  1541.  
  1542. #ifdef ZIPPY
  1543.     if (appData.zippyPlay && newGame &&
  1544.     gameMode != IcsObserving && gameMode != IcsIdle &&
  1545.     gameMode != IcsExamining)
  1546.       ZippyFirstBoard(moveNum, basetime, increment);
  1547. #endif
  1548.  
  1549.     /* Put the move on the move list, first converting
  1550.        to canonical algebraic form. */
  1551.     if (moveNum > 0) {
  1552.     ChessMove moveType;
  1553.     int fromX, fromY, toX, toY;
  1554.     char promoChar;
  1555.  
  1556.     fromX = fromY = toX = toY = -1;
  1557.     if (moveNum <= backwardMostMove) {
  1558.         /* We don't know what the board looked like before
  1559.            this move.  Punt. */
  1560.         strcpy(parseList[moveNum - 1], move_str);
  1561.         strcat(parseList[moveNum - 1], " ");
  1562.         strcat(parseList[moveNum - 1], elapsed_time);
  1563.     } else if (ParseMachineMove(move_str, moveNum - 1, &moveType,
  1564.                     &fromX, &fromY, &toX, &toY, &promoChar)) {
  1565.         moveType = CoordsToAlgebraic(boards[moveNum - 1],
  1566.                      FakeFlags(moveNum - 1), EP_UNKNOWN,
  1567.                      fromY, fromX, toY, toX, promoChar,
  1568.                      parseList[moveNum-1]);
  1569.         strcat(parseList[moveNum - 1], " ");
  1570.         strcat(parseList[moveNum - 1], elapsed_time);
  1571.     } else if (strcmp(move_str, "none") == 0) {
  1572.         /* Again, we don't know what the board looked like;
  1573.            this is really the start of the game. */
  1574.         /* !!What should Zippy do if this happens? */
  1575.         /* !!Actually, I don't think this can happen currently. */
  1576.         parseList[moveNum - 1][0] = NULLCHAR;
  1577.         backwardMostMove = moveNum;
  1578.         startedFromSetupPosition = TRUE;
  1579.     } else {
  1580.         /* Move from ICS was illegal!?  Punt. */
  1581.         if (appData.debugMode) {
  1582.         sprintf(str, "Illegal move \"%s\" from ICS", move_str);
  1583.         DisplayError(str, 0);
  1584.         }
  1585.         strcpy(parseList[moveNum - 1], move_str);
  1586.         strcat(parseList[moveNum - 1], " ");
  1587.         strcat(parseList[moveNum - 1], elapsed_time);
  1588.     }
  1589.  
  1590. #ifdef ZIPPY
  1591.     /* Send move to chess program */
  1592.     if (appData.zippyPlay && !newGame) {
  1593.         if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
  1594.         (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
  1595.  
  1596.         if (fromX == -1) {
  1597.             sprintf(str, "Couldn't parse move \"%s\" from ICS",
  1598.                 move_str);
  1599.             DisplayError(str, 0);
  1600.         } else {
  1601.             SendMoveToProgram(moveType, fromX, fromY, toX, toY,
  1602.                       firstProgramPR, firstSendTime);
  1603.             if (firstMove) {
  1604.             firstMove = FALSE;
  1605.             SendToProgram(appData.whiteString, firstProgramPR);
  1606.             }
  1607.         }
  1608.         }
  1609.     }
  1610. #endif
  1611.     }
  1612.     
  1613.     /* Start the clocks */
  1614.     appData.clockMode = !(ics_basetime == 0 && ics_increment == 0);
  1615.     if (gameMode == IcsIdle ||
  1616.     relation == RELATION_OBSERVING_STATIC ||
  1617.     relation == RELATION_EXAMINING ||
  1618.     ics_clock_paused)
  1619.       DisplayBothClocks();
  1620.     else
  1621.       StartClocks();
  1622.  
  1623.     /* Display opponents and material strengths */
  1624.     sprintf(str, "%s (%d) vs. %s (%d)",
  1625.         gameInfo.white, white_stren, gameInfo.black, black_stren);
  1626.     DisplayTitle(str);
  1627.     
  1628.     /* Display the board */
  1629.     if (!pausing) {
  1630.     DrawPosition(FALSE, boards[currentMove]);
  1631.     DisplayMove(moveNum - 1);
  1632.     if (appData.ringBellAfterMoves && !ics_user_moved)
  1633.       RingBell();
  1634.     }
  1635. }
  1636.  
  1637.  
  1638. void SendMoveToICS(moveType, fromX, fromY, toX, toY)
  1639.      ChessMove moveType;
  1640.      int fromX, fromY, toX, toY;
  1641. {
  1642.     char user_move[MSG_SIZ];
  1643.     char promo_char;
  1644.  
  1645.     switch (moveType) {
  1646.       default:
  1647.     DisplayError("Internal error; bad moveType", 0);
  1648.     break;
  1649.       case WhiteKingSideCastle:
  1650.       case BlackKingSideCastle:
  1651.       case WhiteQueenSideCastleWild:
  1652.       case BlackQueenSideCastleWild:
  1653.     sprintf(user_move, "o-o\n");
  1654.     break;
  1655.       case WhiteQueenSideCastle:
  1656.       case BlackQueenSideCastle:
  1657.       case WhiteKingSideCastleWild:
  1658.       case BlackKingSideCastleWild:
  1659.     sprintf(user_move, "o-o-o\n");
  1660.     break;
  1661.       case WhitePromotionQueen:
  1662.       case BlackPromotionQueen:
  1663.       case WhitePromotionRook:
  1664.       case BlackPromotionRook:
  1665.       case WhitePromotionBishop:
  1666.       case BlackPromotionBishop:
  1667.       case WhitePromotionKnight:
  1668.       case BlackPromotionKnight:
  1669. #if 0 
  1670.     sprintf(user_move, "%c%c%c%c=%c\n",
  1671.         'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY,
  1672.         PieceToChar(PromoPiece(moveType)));
  1673. #else /* temporary code for FICS compatibility */
  1674.     promo_char = PieceToChar(PromoPiece(moveType));
  1675.     if (promo_char == 'n') promo_char = 'k';  /* for ICC (!) */
  1676.     sprintf(user_move, "promote %c\n%c%c%c%c\n",
  1677.         PieceToChar(PromoPiece(moveType)),
  1678.         'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY);
  1679. #endif
  1680.     break;
  1681.       case NormalMove:
  1682.       case WhiteCapturesEnPassant:
  1683.       case BlackCapturesEnPassant:
  1684.     sprintf(user_move, "%c%c%c%c\n",
  1685.         'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY);
  1686.     break;
  1687.     }
  1688.     SendToICS(user_move);
  1689. }
  1690.  
  1691. void ProcessICSInitScript(f)
  1692.      FILE *f;
  1693. {
  1694.     char buf[MSG_SIZ];
  1695.  
  1696.     while (fgets(buf, MSG_SIZ, f)) {
  1697.     SendToICS(buf);
  1698.     }
  1699.  
  1700.     fclose(f);
  1701. }
  1702.  
  1703.  
  1704. /* Parser for moves from gnuchess or ICS */
  1705. Boolean ParseMachineMove(machineMove, moveNum,
  1706.              moveType, fromX, fromY, toX, toY, promoChar)
  1707.      char *machineMove;
  1708.      int moveNum;
  1709.      ChessMove *moveType;
  1710.      int *fromX, *fromY, *toX, *toY;
  1711.      char *promoChar;
  1712. {       
  1713.     *moveType = yylexstr(moveNum, machineMove);
  1714.     switch (*moveType) {
  1715.       case WhitePromotionQueen:
  1716.       case BlackPromotionQueen:
  1717.       case WhitePromotionRook:
  1718.       case BlackPromotionRook:
  1719.       case WhitePromotionBishop:
  1720.       case BlackPromotionBishop:
  1721.       case WhitePromotionKnight:
  1722.       case BlackPromotionKnight:
  1723.       case NormalMove:
  1724.       case WhiteCapturesEnPassant:
  1725.       case BlackCapturesEnPassant:
  1726.       case WhiteKingSideCastle:
  1727.       case WhiteQueenSideCastle:
  1728.       case BlackKingSideCastle:
  1729.       case BlackQueenSideCastle:
  1730.       case WhiteKingSideCastleWild:
  1731.       case WhiteQueenSideCastleWild:
  1732.       case BlackKingSideCastleWild:
  1733.       case BlackQueenSideCastleWild:
  1734.     *fromX = currentMoveString[0] - 'a';
  1735.     *fromY = currentMoveString[1] - '1';
  1736.     *toX = currentMoveString[2] - 'a';
  1737.     *toY = currentMoveString[3] - '1';
  1738.     *promoChar = currentMoveString[4];
  1739.     return TRUE;
  1740.  
  1741.       case BadMove:
  1742.     /* bug?  Try parsing as coordinate notation. */
  1743.     *fromX = machineMove[0] - 'a';
  1744.     *fromY = machineMove[1] - '1';
  1745.     *toX = machineMove[2] - 'a';
  1746.     *toY = machineMove[3] - '1';
  1747.     *promoChar = machineMove[4];
  1748.     if (*fromX < 0 || *fromX > 7 || *fromY < 0 || *fromY > 7 ||
  1749.         *toX < 0 || *toX > 7 || *toY < 0 || *toY > 7)
  1750.       *fromX = *fromY = *toX = *toY = 0;
  1751.     return FALSE;
  1752.  
  1753.       case AmbiguousMove:
  1754.       case (ChessMove) 0:    /* end of file */
  1755.       case ElapsedTime:
  1756.       case Comment:
  1757.       case PGNTag:
  1758.       case WhiteWins:
  1759.       case BlackWins:
  1760.       case GameIsDrawn:
  1761.       default:
  1762.     /* bug? */
  1763.     *fromX = *fromY = *toX = *toY = 0;
  1764.     *promoChar = NULLCHAR;
  1765.     return FALSE;
  1766.     }
  1767. }
  1768.  
  1769.  
  1770. void InitPosition(redraw)
  1771.      int redraw;
  1772. {
  1773.     currentMove = forwardMostMove = backwardMostMove = 0;
  1774.     CopyBoard(boards[0], initialPosition);
  1775.     if (redraw)
  1776.       DrawPosition(FALSE, boards[currentMove]);
  1777. }
  1778.  
  1779. void SendCurrentBoard(pr)
  1780.      ProcRef pr;
  1781. {
  1782.     SendBoard(pr, boards[currentMove]);
  1783. }
  1784.  
  1785. void SendBoard(pr, board)
  1786.      ProcRef pr;
  1787.      Board board;
  1788. {
  1789.     char message[MSG_SIZ];
  1790.     ChessSquare *bp;
  1791.     int i, j;
  1792.     
  1793.     SendToProgram("edit\n", pr);
  1794.     SendToProgram("#\n", pr);
  1795.     for (i = BOARD_SIZE - 1; i >= 0; i--) {
  1796.     bp = &board[i][0];
  1797.     for (j = 0; j < BOARD_SIZE; j++, bp++) {
  1798.         if ((int) *bp < (int) BlackPawn) {
  1799.         sprintf(message, "%c%c%c\n", PieceToChar(*bp), 
  1800.             'a' + j, '1' + i);
  1801.         SendToProgram(message, pr);
  1802.         }
  1803.     }
  1804.     }
  1805.     
  1806.     SendToProgram("c\n", pr);
  1807.     for (i = BOARD_SIZE - 1; i >= 0; i--) {
  1808.     bp = &board[i][0];
  1809.     for (j = 0; j < BOARD_SIZE; j++, bp++) {
  1810.         if (((int) *bp != (int) EmptySquare)
  1811.         && ((int) *bp >= (int) BlackPawn)) {
  1812.         sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
  1813.             'a' + j, '1' + i);
  1814.         SendToProgram(message, pr);
  1815.         }
  1816.     }
  1817.     }
  1818.     
  1819.     SendToProgram(".\n", pr);
  1820. }
  1821.  
  1822.  
  1823. void SendMoveToProgram(moveType, fromX, fromY, toX, toY, programPR, sendTime)
  1824.      ChessMove moveType;
  1825.      int fromX, fromY, toX, toY;
  1826.      ProcRef programPR;
  1827.      int sendTime;
  1828. {
  1829.     char user_move[MSG_SIZ];
  1830.     char promoChar = ToLower(PieceToChar(PromoPiece(moveType)));
  1831.  
  1832.     if (promoChar == '.')
  1833.       sprintf(user_move, "%c%c%c%c\n",
  1834.           'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY);
  1835.     else
  1836.       sprintf(user_move, "%c%c%c%c%c\n",
  1837.           'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY,
  1838.           promoChar);
  1839.     
  1840.     if (sendTime)
  1841.       SendTimeRemaining(programPR);
  1842.     SendToProgram(user_move, programPR);
  1843.     strcpy(moveList[currentMove - 1], user_move);  /* note side-effect */
  1844. }
  1845.  
  1846.  
  1847. int IsPromotion(fromX, fromY, toX, toY)
  1848.      int fromX, fromY, toX, toY;
  1849. {
  1850.     return gameMode != EditPosition &&
  1851.       fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0 &&
  1852.     ((boards[currentMove][fromY][fromX] == WhitePawn && toY == 7) ||
  1853.      (boards[currentMove][fromY][fromX] == BlackPawn && toY == 0));
  1854. }
  1855.  
  1856.  
  1857. int OKToStartUserMove(x, y)
  1858.      int x, y;
  1859. {
  1860.     ChessSquare from_piece;
  1861.     int white_piece;
  1862.  
  1863.     if (matchMode) return FALSE;
  1864.     if (gameMode == EditPosition) return TRUE;
  1865.  
  1866.     if (x >= 0 && y >= 0)
  1867.       from_piece = boards[currentMove][y][x];
  1868.     else
  1869.       from_piece = EmptySquare;
  1870.  
  1871.     if (from_piece == EmptySquare) return FALSE;
  1872.  
  1873.     white_piece = (int)from_piece >= (int)WhitePawn &&
  1874.                   (int)from_piece <= (int)WhiteKing;
  1875.  
  1876.     switch (gameMode) {
  1877.       case PlayFromGameFile:
  1878.       case TwoMachinesPlay:
  1879.       case EndOfGame:
  1880.     return FALSE;
  1881.  
  1882.       case IcsObserving:
  1883.       case IcsIdle:
  1884.     return FALSE;
  1885.  
  1886.       case MachinePlaysWhite:
  1887.       case IcsPlayingBlack:
  1888.     if (appData.zippyPlay) return FALSE;
  1889.     if (white_piece) {
  1890.         DisplayError("You are playing Black", 0);
  1891.         return FALSE;
  1892.     }
  1893.     break;
  1894.  
  1895.       case MachinePlaysBlack:
  1896.       case IcsPlayingWhite:
  1897.     if (appData.zippyPlay) return FALSE;
  1898.     if (!white_piece) {
  1899.         DisplayError("You are playing White", 0);
  1900.         return FALSE;
  1901.     }
  1902.     break;
  1903.  
  1904.       case EditGame:
  1905.     if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
  1906.         /* Editing correspondence game history */
  1907.         /* Could disallow this or prompt for confirmation */
  1908.         cmailOldMove = -1;
  1909.     }
  1910.     if (currentMove < forwardMostMove) {
  1911.         /* Discarding moves */
  1912.         /* Could prompt for confirmation here,
  1913.            but I don't think that's such a good idea */
  1914.         forwardMostMove = currentMove;
  1915.     }
  1916.     break;
  1917.  
  1918.       case BeginningOfGame:
  1919.     if (appData.icsActive) return FALSE;
  1920.     if (!appData.noChessProgram) {
  1921.         if (!white_piece) {
  1922.         DisplayError("You are playing White", 0);
  1923.         return FALSE;
  1924.         }
  1925.     }
  1926.     break;
  1927.  
  1928.       default:
  1929.       case IcsExamining:
  1930.     break;
  1931.     }
  1932.     if (currentMove != forwardMostMove) {
  1933.     DisplayError("Displayed position is not current", 0);
  1934.     return FALSE;
  1935.     }
  1936.     return TRUE;
  1937. }
  1938.  
  1939. FILE *lastLoadGameFP = NULL;
  1940. int lastLoadGameNumber = 0;
  1941. int lastLoadGameUseList = FALSE;
  1942. char lastLoadGameTitle[MSG_SIZ];
  1943. ChessMove lastLoadGameStart = (ChessMove) 0;
  1944.  
  1945. void UserMoveEvent(fromX, fromY, toX, toY, promoChar)
  1946.      int fromX, fromY, toX, toY;
  1947.      int promoChar;
  1948. {
  1949.     ChessMove moveType;
  1950.     
  1951.     if (fromX < 0 || fromY < 0) return;
  1952.     if ((fromX == toX) && (fromY == toY)) {
  1953.     return;
  1954.     }
  1955.     
  1956.     if (toX < 0 || toY < 0) {
  1957.     if (gameMode == EditPosition && (toX == -2 || toY == -2)) {
  1958.         boards[0][fromY][fromX] = EmptySquare;
  1959.         DrawPosition(FALSE, boards[currentMove]);
  1960.     }
  1961.     return;
  1962.     }
  1963.     
  1964.     if (gameMode == EditPosition) {
  1965.     boards[0][toY][toX] = boards[0][fromY][fromX];
  1966.     boards[0][fromY][fromX] = EmptySquare;
  1967.     DrawPosition(FALSE, boards[currentMove]);
  1968.     return;
  1969.     }
  1970.     
  1971.     /* Check if the user is playing in turn.  This is complicated because we
  1972.        let the user "pick up" a piece before it is his turn.  So the piece he
  1973.        tried to pick up may have been captured by the time he puts it down!
  1974.        Therefore we use the color the user is supposed to be playing in this
  1975.        test, not the color of the piece that is currently on the starting
  1976.        square---except in EditGame mode, where the user is playing both
  1977.        sides; fortunately there the capture race can't happen.  (It can
  1978.        now happen in IcsExamining mode, but that's just too bad.  The user
  1979.        will get a somewhat confusing message in that case.)
  1980.     */
  1981.     if (gameMode == MachinePlaysWhite || gameMode == IcsPlayingBlack ||
  1982.     ((gameMode == EditGame || gameMode == BeginningOfGame ||
  1983.       gameMode == IcsExamining) && 
  1984.      (int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
  1985.      (int) boards[currentMove][fromY][fromX] <= (int) BlackKing)) {
  1986.     /* User is moving for Black */
  1987.     if (WhiteOnMove(currentMove)) {
  1988.         DisplayError("It is White's turn", 0);
  1989.         return;
  1990.     }
  1991.     } else {
  1992.     /* User is moving for White */
  1993.     if (!WhiteOnMove(currentMove)) {
  1994.         DisplayError("It is Black's turn", 0);
  1995.         return;
  1996.     }
  1997.     }
  1998.  
  1999.     moveType = LegalityTest(boards[currentMove], FakeFlags(currentMove),
  2000.                 EP_UNKNOWN, fromY, fromX, toY, toX, promoChar);
  2001.  
  2002.     if (moveType == BadMove) {
  2003.     DisplayError("Illegal move", 0);
  2004.     return;
  2005.     }
  2006.  
  2007.     /* If we need the chess program but it's dead, restart it */
  2008.     ResurrectChessProgram();
  2009.  
  2010.     /* A user move restarts a paused game*/
  2011.     if (pausing)
  2012.       PauseEvent();
  2013.     
  2014.     thinkOutput[0] = NULLCHAR;
  2015.     MakeMove(&moveType, fromX, fromY, toX, toY);
  2016.  
  2017.     if (appData.icsActive) {
  2018.     if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
  2019.         gameMode == IcsExamining) {
  2020.         SendMoveToICS(moveType, fromX, fromY, toX, toY);
  2021.         ics_user_moved = 1;
  2022.     }
  2023.     } else {
  2024.     SendMoveToProgram(moveType, fromX, fromY, toX, toY,
  2025.               firstProgramPR, firstSendTime);
  2026.  
  2027.     if (currentMove == cmailOldMove + 1) {
  2028.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
  2029.     }
  2030.     }
  2031.     
  2032.     switch (gameMode) {
  2033.       case BeginningOfGame:
  2034.     if (appData.noChessProgram)
  2035.       lastGameMode = gameMode = EditGame;
  2036.     else
  2037.       lastGameMode = gameMode = MachinePlaysBlack;
  2038.     SetGameInfo();
  2039.     ModeHighlight();
  2040.     break;
  2041.  
  2042.       case EditGame:
  2043.     switch (MateTest(boards[currentMove], FakeFlags(currentMove),
  2044.              EP_UNKNOWN)) {
  2045.       case MT_NONE:
  2046.       case MT_CHECK:
  2047.         break;
  2048.       case MT_CHECKMATE:
  2049.         if (WhiteOnMove(currentMove)) {
  2050.         GameEnds(BlackWins, "Black mates", GE_PLAYER);
  2051.         } else {
  2052.         GameEnds(WhiteWins, "White mates", GE_PLAYER);
  2053.         }
  2054.         break;
  2055.       case MT_STALEMATE:
  2056.         GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
  2057.         break;
  2058.     }
  2059.     break;
  2060.  
  2061.       case MachinePlaysBlack:
  2062.       case MachinePlaysWhite:
  2063.       default:
  2064.     break;
  2065.     }
  2066. }
  2067.  
  2068. void HandleMachineMove(message, isr)
  2069.      char *message;
  2070.      InputSourceRef isr;
  2071. {
  2072.     char machineMove[MSG_SIZ], buf1[MSG_SIZ], buf2[MSG_SIZ];
  2073.     int fromX, fromY, toX, toY;
  2074.     ChessMove moveType;
  2075.     char promoChar;
  2076.     
  2077.     if (strncmp(message, "warning:", 8) == 0) {
  2078.     DisplayError(message, 0);
  2079.     return;
  2080.     }
  2081.     
  2082.     /*
  2083.      * If chess program startup fails, exit with an error message.
  2084.      * Attempts to recover here are futile.
  2085.      */
  2086.     if ((StrStr(message, "unknown host") != NULL)
  2087.     || (StrStr(message, "No remote directory") != NULL)
  2088.     || (StrStr(message, "not found") != NULL)
  2089.     || (StrStr(message, "No such file") != NULL)
  2090.     || (StrStr(message, "can't alloc") != NULL)
  2091.     || (StrStr(message, "Permission denied") != NULL)) {
  2092.     maybePondering = FALSE;
  2093.     sprintf(buf1,
  2094.         "Failed to start chess program %s on %s: %s\n",
  2095.         isr == firstProgramISR ? appData.firstChessProgram
  2096.         : appData.secondChessProgram,
  2097.         isr == firstProgramISR ? appData.firstHost
  2098.         : appData.secondHost,
  2099.         message);
  2100.     RemoveInputSource(isr);
  2101.     DisplayFatalError(buf1, 0, 1);
  2102.     return;
  2103.     }
  2104.     
  2105.     /*
  2106.      * If the move is illegal, cancel it and redraw the board.
  2107.      */
  2108.     if (strncmp(message, "Illegal move", 12) == 0) {
  2109.     maybePondering = FALSE;
  2110.     if (StrStr(message, "time") || StrStr(message, "otim") ||
  2111.         StrStr(message, timeTestStr)) {
  2112.         if (isr == firstProgramISR) {
  2113.         /* First program doesn't have "time" or "otim" command */
  2114.         firstSendTime = 0;
  2115.         return;
  2116.         } else if (isr == secondProgramISR) {
  2117.         /* Second program doesn't have "time" or "otim" command */
  2118.         secondSendTime = 0;
  2119.         return;
  2120.         }
  2121.     }
  2122.     if (forwardMostMove <= backwardMostMove) return;
  2123.     if (WhiteOnMove(forwardMostMove) !=
  2124.         ( (gameMode == MachinePlaysWhite && isr == firstProgramISR) ||
  2125.           (gameMode == TwoMachinesPlay && isr == secondProgramISR) )) {
  2126.         /* Bogus complaint about gnuchess's own hint move; gnuchess bug */
  2127. #ifndef ZIPPY
  2128.         sprintf(buf1, "%s chess program gave bogus error message:\n %s",
  2129.             isr == firstProgramISR ? "first" : "second", message);
  2130.         DisplayFatalError(buf1, 0, -1); /* don't exit */
  2131. #endif /*ZIPPY*/
  2132.         return;
  2133.     }
  2134.     if (pausing) PauseEvent();
  2135.     if (gameMode == PlayFromGameFile) {
  2136.         /* Stop reading this game file */
  2137.         gameMode = EditGame;
  2138.         ModeHighlight();
  2139.     }
  2140.     currentMove = --forwardMostMove;
  2141.     SwitchClocks();
  2142.     sprintf(buf1, "Illegal move \"%s\"\n(rejected by %schess program)",
  2143.         parseList[currentMove],
  2144.         isr == firstProgramISR ? "first " :
  2145.         (isr == secondProgramISR ? "second " : ""));
  2146.     DisplayError(buf1, 0);
  2147.     
  2148.     DrawPosition(FALSE, boards[currentMove]);
  2149.     return;
  2150.     }
  2151.     
  2152.     if (strncmp(message, "time", 4) == 0 && StrStr(message, "CHESS")) {
  2153.     /* Program has a broken "time" command that
  2154.        outputs a string not ending in newline.
  2155.        Don't use it. */
  2156.     if (isr == firstProgramISR) firstSendTime = 0;
  2157.     if (isr == secondProgramISR) secondSendTime = 0;
  2158.     }
  2159.     
  2160.     if (strncmp(message, "Hint:", 5) == 0) {
  2161.     if (hintRequested) {
  2162.         hintRequested = FALSE;
  2163.         sscanf(message, "Hint: %s", machineMove);
  2164.         if (ParseMachineMove(machineMove, forwardMostMove, &moveType,
  2165.                  &fromX, &fromY, &toX, &toY, &promoChar)) {
  2166.         moveType =
  2167.           CoordsToAlgebraic(boards[forwardMostMove],
  2168.                     FakeFlags(forwardMostMove), EP_UNKNOWN,
  2169.                     fromY, fromX, toY, toX, promoChar, buf1);
  2170.         sprintf(buf2, "Hint: %s", buf1);
  2171.         DisplayInformation(buf2);
  2172.         } else {
  2173.         /* Hint move was illegal!? */
  2174.         sprintf(buf1, "Illegal hint move \"%s\"\nfrom %schess program",
  2175.             machineMove, isr == firstProgramISR ? "first " :
  2176.             (isr == secondProgramISR ? "second " : ""));
  2177.         DisplayError(buf1, 0);
  2178.         }
  2179.     }
  2180.     return;
  2181.     }
  2182.     
  2183.     /*
  2184.      * win, lose or draw
  2185.      */
  2186.     if (strncmp(message, "White", 5) == 0) {
  2187.     GameEnds(WhiteWins, "White mates", GE_GNU);
  2188.     return;
  2189.     } else if (strncmp(message, "Black", 5) == 0) {
  2190.     GameEnds(BlackWins, "Black mates", GE_GNU);
  2191.     return;
  2192.     } else if (strncmp(message, "opponent mates!", 15) == 0) {
  2193.     switch (gameMode) {
  2194.       case MachinePlaysBlack:
  2195.         GameEnds(WhiteWins, "White mates", GE_GNU);
  2196.         break;
  2197.       case MachinePlaysWhite:
  2198.         GameEnds(BlackWins, "Black mates", GE_GNU);
  2199.         break;
  2200.       case TwoMachinesPlay:
  2201.         if (isr == firstProgramISR)
  2202.           GameEnds(WhiteWins, "White mates", GE_GNU);
  2203.         else
  2204.           GameEnds(BlackWins, "Black mates", GE_GNU);
  2205.         break;
  2206.       default:
  2207.         /* can't happen */
  2208.         break;
  2209.     }
  2210.     return;
  2211.     } else if (strncmp(message, "computer mates!", 15) == 0) {
  2212.     switch (gameMode) {
  2213.       case MachinePlaysBlack:
  2214.         GameEnds(BlackWins, "Black mates", GE_GNU);
  2215.         break;
  2216.       case MachinePlaysWhite:
  2217.         GameEnds(WhiteWins, "White mates", GE_GNU);
  2218.         break;
  2219.       case TwoMachinesPlay:
  2220.         if (isr == firstProgramISR)
  2221.           GameEnds(BlackWins, "Black mates", GE_GNU);
  2222.         else
  2223.           GameEnds(WhiteWins, "White mates", GE_GNU);
  2224.         break;
  2225.       default:
  2226.         /* can't happen */
  2227.         break;
  2228.     }
  2229.     return;
  2230.     } else if (strncmp(message, "Draw", 4) == 0) {
  2231. #ifdef ZIPPY
  2232.     if (appData.zippyPlay) {
  2233.         /* If this was a draw by repetition or the
  2234.            50-move rule, we have to claim it.
  2235.            */
  2236.         SendToICS("draw\n");
  2237.     }
  2238. #endif
  2239.     GameEnds(GameIsDrawn, "Draw", GE_GNU);
  2240.     return;
  2241.     }    
  2242.  
  2243.     if (appData.showThinking) {
  2244.     int plylev, curscore, time, nodes;
  2245.     char plyext;
  2246.     if (sscanf(message, "%d%c %d %d %d",
  2247.            &plylev, &plyext, &curscore, &time, &nodes) == 5) {
  2248.         sprintf(thinkOutput, "%s%+.2f  %s",
  2249.             gameMode == TwoMachinesPlay ?
  2250.               (isr == firstProgramISR ? "B" : "W") : "",
  2251.             ((double) curscore) / 100.0, &message[27]);
  2252.         if (currentMove == forwardMostMove)
  2253.           DisplayMove(currentMove - 1);
  2254.         return;
  2255.     } else if (thinkOutput[0] != NULLCHAR &&
  2256.            strncmp(message, "                           ", 27) == 0) {
  2257.         strcat(thinkOutput, &message[26]);
  2258.         if (currentMove == forwardMostMove)
  2259.           DisplayMove(currentMove - 1);
  2260.         return;
  2261.     }
  2262.     }
  2263.  
  2264.     if (bookRequested) {
  2265.     if (message[0] == '\t' || strncmp(message, "        ", 8) == 0) {
  2266.         sscanf(message, "%s %s %s", machineMove, buf1, buf2);
  2267.         sprintf(&bookOutput[strlen(bookOutput)], "%s   %s   %s    \n",
  2268.             machineMove, buf1, buf2);
  2269.     } else if (bookOutput[0] != NULLCHAR) {
  2270.         DisplayInformation(bookOutput);
  2271.         bookRequested = FALSE;
  2272.     }
  2273.     }
  2274.  
  2275.     if (StrStr(message, "...") != NULL) {
  2276.     /* Normal machine reply move */
  2277.     sscanf(message, "%s %s %s", buf1, buf2, machineMove);
  2278.     if (machineMove[0] == NULLCHAR)
  2279.       return;
  2280.     /* Fall through to code below */
  2281.     } else {
  2282.     return;        /* ignore noise */
  2283.     }
  2284.  
  2285.     /*
  2286.      * Normal machine reply move
  2287.      */
  2288.     hintRequested = FALSE;
  2289.     bookRequested = FALSE;
  2290.     maybePondering = TRUE;
  2291.     if (firstSendTime == 2 && isr == firstProgramISR) firstSendTime = 1;
  2292.     if (secondSendTime == 2 && isr == secondProgramISR) secondSendTime = 1;
  2293.  
  2294.     if (!ParseMachineMove(machineMove, forwardMostMove, &moveType,
  2295.               &fromX, &fromY, &toX, &toY, &promoChar)) {
  2296.     /* Machine move was illegal!?  Give message and continue. */
  2297.     sprintf(buf1, "Illegal move \"%s\" from machine",
  2298.         machineMove);
  2299.     DisplayError(buf1, 0);
  2300.     }
  2301.     
  2302.     strcat(machineMove, "\n");
  2303.     strcpy(moveList[forwardMostMove], machineMove);
  2304.     
  2305.     if (!pausing)
  2306.       currentMove = forwardMostMove;  /*display latest move*/
  2307.     
  2308.     MakeMove(&moveType, fromX, fromY, toX, toY);
  2309.     
  2310.     if (!pausing && appData.ringBellAfterMoves)
  2311.       RingBell();
  2312.     
  2313. #ifdef ZIPPY
  2314.     if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) {
  2315.     SendMoveToICS(moveType, fromX, fromY, toX, toY);
  2316.     ics_user_moved = 1;
  2317.     }
  2318. #endif
  2319.  
  2320.     if (gameMode == TwoMachinesPlay) {
  2321.     if (WhiteOnMove(forwardMostMove)) {
  2322.         if (secondSendTime) 
  2323.           SendTimeRemaining(secondProgramPR);
  2324.         SendToProgram(machineMove, secondProgramPR);
  2325.         if (firstMove) {
  2326.         firstMove = FALSE;
  2327.         SendToProgram(appData.whiteString, secondProgramPR);
  2328.         }
  2329.     } else {
  2330.         if (firstSendTime)
  2331.           SendTimeRemaining(firstProgramPR);
  2332.         SendToProgram(machineMove, firstProgramPR);
  2333.         if (firstMove) {
  2334.         firstMove = FALSE;
  2335.         SendToProgram(appData.blackString, firstProgramPR);
  2336.         }
  2337.     }
  2338.     }
  2339. }
  2340.  
  2341.  
  2342. /* Parse a game score from the character string "game", and
  2343.    record it as the history of the current game.  The game
  2344.    score is NOT assumed to start from the standard position. 
  2345.    The display is not updated in any way.
  2346.    */
  2347. void ParseGameHistory(game)
  2348.      char *game;
  2349. {
  2350.     ChessMove moveType;
  2351.     int fromX, fromY, toX, toY, boardIndex;
  2352.     char promoChar;
  2353.     char *p, *q;
  2354.     char buf[MSG_SIZ];
  2355.  
  2356.     if (appData.debugMode)
  2357.       fprintf(debugFP, "Parsing game history: %s\n", game);
  2358.  
  2359.     gameInfo.event = StrSave("ICS game");
  2360.     gameInfo.site = StrSave(appData.icsHost);
  2361.     gameInfo.date = PGNDate();
  2362.     gameInfo.round = StrSave("-");
  2363.  
  2364.     /* Parse out names of players */
  2365.     while (*game == ' ') game++;
  2366.     p = buf;
  2367.     while (*game != ' ') *p++ = *game++;
  2368.     *p = NULLCHAR;
  2369.     gameInfo.white = StrSave(buf);
  2370.     while (*game == ' ') game++;
  2371.     p = buf;
  2372.     while (*game != ' ' && *game != '\n') *p++ = *game++;
  2373.     *p = NULLCHAR;
  2374.     gameInfo.black = StrSave(buf);
  2375.  
  2376.     /* Parse moves */
  2377.     boardIndex = 0;
  2378.     yynewstr(game);
  2379.     for (;;) {
  2380.     yyboardindex = boardIndex;
  2381.     moveType = (ChessMove) yylex();
  2382.     switch (moveType) {
  2383.       case WhitePromotionQueen:
  2384.       case BlackPromotionQueen:
  2385.       case WhitePromotionRook:
  2386.       case BlackPromotionRook:
  2387.       case WhitePromotionBishop:
  2388.       case BlackPromotionBishop:
  2389.       case WhitePromotionKnight:
  2390.       case BlackPromotionKnight:
  2391.       case NormalMove:
  2392.       case WhiteCapturesEnPassant:
  2393.       case BlackCapturesEnPassant:
  2394.       case WhiteKingSideCastle:
  2395.       case WhiteQueenSideCastle:
  2396.       case BlackKingSideCastle:
  2397.       case BlackQueenSideCastle:
  2398.       case WhiteKingSideCastleWild:
  2399.       case WhiteQueenSideCastleWild:
  2400.       case BlackKingSideCastleWild:
  2401.       case BlackQueenSideCastleWild:
  2402.         fromX = currentMoveString[0] - 'a';
  2403.         fromY = currentMoveString[1] - '1';
  2404.         toX = currentMoveString[2] - 'a';
  2405.         toY = currentMoveString[3] - '1';
  2406.         promoChar = currentMoveString[4];
  2407.         break;
  2408.       case BadMove:
  2409.         /* bug? */
  2410.         sprintf(buf, "Bad move in ICS output: \"%s\"", yy_text);
  2411.         DisplayError(buf, 0);
  2412.         return;
  2413.       case AmbiguousMove:
  2414.         /* bug? */
  2415.         sprintf(buf, "Ambiguous move in ICS output: \"%s\"", yy_text);
  2416.         DisplayError(buf, 0);
  2417.         return;
  2418.       case (ChessMove) 0:    /* end of file */
  2419.         if (boardIndex < backwardMostMove) {
  2420.         /* Oops, gap.  How did that happen? */
  2421.         return;
  2422.         }
  2423.         backwardMostMove = 0;
  2424.         if (boardIndex > forwardMostMove) {
  2425.         forwardMostMove = boardIndex;
  2426.         }
  2427.         return;
  2428.       case ElapsedTime:
  2429.         if (boardIndex > 0) {
  2430.         strcat(parseList[boardIndex-1], " ");
  2431.         strcat(parseList[boardIndex-1], yy_text);
  2432.         }
  2433.         continue;
  2434.       case Comment:
  2435.       case PGNTag:
  2436.       default:
  2437.         /* ignore */
  2438.         continue;
  2439.       case WhiteWins:
  2440.       case BlackWins:
  2441.       case GameIsDrawn:
  2442.       case GameUnfinished:
  2443.         if (gameMode == IcsExamining) {
  2444.         if (boardIndex < backwardMostMove) {
  2445.             /* Oops, gap.  How did that happen? */
  2446.             return;
  2447.         }
  2448.         backwardMostMove = 0;
  2449.         return;
  2450.         }
  2451.         gameInfo.result = moveType;
  2452.         p = strchr(yy_text, '{');
  2453.         if (p == NULL) p = strchr(yy_text, '(');
  2454.         if (p == NULL) {
  2455.         p = yy_text;
  2456.         if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
  2457.         } else {
  2458.         q = strchr(p, *p == '{' ? '}' : ')');
  2459.         if (q != NULL) *q = NULLCHAR;
  2460.         p++;
  2461.         }
  2462.         gameInfo.resultDetails = StrSave(p);
  2463.         continue;
  2464.     }
  2465.     if (boardIndex >= forwardMostMove &&
  2466.         !(gameMode == IcsObserving && ics_gamenum == -1)) {
  2467.         backwardMostMove = 0;
  2468.         return;
  2469.     }
  2470.     (void) CoordsToAlgebraic(boards[boardIndex], FakeFlags(boardIndex),
  2471.                  EP_UNKNOWN, fromY, fromX, toY, toX, promoChar,
  2472.                  parseList[boardIndex]);
  2473.     CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
  2474.     boardIndex++;
  2475.     ApplyMove(&moveType, fromX, fromY, toX, toY,
  2476.           boards[boardIndex]);
  2477.     }
  2478. }
  2479.  
  2480.  
  2481. /* Apply a move to the given board.  Oddity: moveType is ignored on
  2482.    input unless the move is seen to be a pawn promotion, in which case
  2483.    moveType tells us what to promote to.
  2484. */
  2485. void ApplyMove(moveType, fromX, fromY, toX, toY, board)
  2486.      ChessMove *moveType;
  2487.      int fromX, fromY, toX, toY;
  2488.      Board board;
  2489. {
  2490.     if (fromY == 0 && fromX == 4
  2491.     && board[fromY][fromX] == WhiteKing
  2492.     && toY == 0 && toX == 6) {
  2493.     *moveType = WhiteKingSideCastle;
  2494.      board[fromY][fromX] = EmptySquare;
  2495.      board[toY][toX] = WhiteKing;
  2496.      board[fromY][7] = EmptySquare;
  2497.      board[toY][5] = WhiteRook;
  2498.     } else if (fromY == 0 && fromX == 4
  2499.            && board[fromY][fromX] == WhiteKing
  2500.            && toY == 0 && toX == 2) {
  2501.     *moveType = WhiteQueenSideCastle;
  2502.      board[fromY][fromX] = EmptySquare;
  2503.      board[toY][toX] = WhiteKing;
  2504.      board[fromY][0] = EmptySquare;
  2505.      board[toY][3] = WhiteRook;
  2506.     } else if (fromY == 0 && fromX == 3
  2507.      && board[fromY][fromX] == WhiteKing
  2508.      && toY == 0 && toX == 5) {
  2509.      *moveType = WhiteKingSideCastleWild;
  2510.      board[fromY][fromX] = EmptySquare;
  2511.      board[toY][toX] = WhiteKing;
  2512.      board[fromY][7] = EmptySquare;
  2513.      board[toY][4] = WhiteRook;
  2514.     } else if (fromY == 0 && fromX == 3
  2515.             && board[fromY][fromX] == WhiteKing
  2516.             && toY == 0 && toX == 1) {
  2517.      *moveType = WhiteQueenSideCastleWild;
  2518.      board[fromY][fromX] = EmptySquare;
  2519.      board[toY][toX] = WhiteKing;
  2520.      board[fromY][0] = EmptySquare;
  2521.      board[toY][2] = WhiteRook;
  2522.     } else if (fromY == 6
  2523.            && board[fromY][fromX] == WhitePawn
  2524.            && toY == 7) {
  2525.     /* white pawn promotion */
  2526.     board[7][toX] = PromoPiece(*moveType);
  2527.     if (board[7][toX] == EmptySquare) {
  2528.         board[7][toX] = WhiteQueen;
  2529.         *moveType = WhitePromotionQueen;
  2530.     }
  2531.     board[6][fromX] = EmptySquare;
  2532.     } else if ((fromY == 4)
  2533.            && (toX != fromX)
  2534.            && (board[fromY][fromX] == WhitePawn)
  2535.            && (board[toY][toX] == EmptySquare)) {
  2536.     *moveType = WhiteCapturesEnPassant;
  2537.     board[fromY][fromX] = EmptySquare;
  2538.     board[toY][toX] = WhitePawn;
  2539.     board[toY - 1][toX] = EmptySquare;
  2540.    } else if (fromY == 7 && fromX == 4
  2541.            && board[fromY][fromX] == BlackKing
  2542.            && toY == 7 && toX == 6) {
  2543.     *moveType = BlackKingSideCastle;
  2544.      board[fromY][fromX] = EmptySquare;
  2545.      board[toY][toX] = BlackKing;
  2546.      board[fromY][7] = EmptySquare;
  2547.      board[toY][5] = BlackRook;
  2548.    } else if (fromY == 7 && fromX == 4
  2549.            && board[fromY][fromX] == BlackKing
  2550.            && toY == 7 && toX == 2) {
  2551.     *moveType = BlackQueenSideCastle;
  2552.      board[fromY][fromX] = EmptySquare;
  2553.      board[toY][toX] = BlackKing;
  2554.      board[fromY][0] = EmptySquare;
  2555.      board[toY][3] = BlackRook;
  2556.     } else if (fromY == 7 && fromX == 3
  2557.             && board[fromY][fromX] == BlackKing
  2558.             && toY == 7 && toX == 5) {
  2559.      *moveType = BlackKingSideCastleWild;
  2560.      board[fromY][fromX] = EmptySquare;
  2561.      board[toY][toX] = BlackKing;
  2562.      board[fromY][7] = EmptySquare;
  2563.      board[toY][4] = BlackRook;
  2564.     } else if (fromY == 7 && fromX == 3
  2565.             && board[fromY][fromX] == BlackKing
  2566.             && toY == 7 && toX == 1) {
  2567.      *moveType = BlackQueenSideCastleWild;
  2568.      board[fromY][fromX] = EmptySquare;
  2569.      board[toY][toX] = BlackKing;
  2570.      board[fromY][0] = EmptySquare;
  2571.      board[toY][2] = BlackRook;
  2572.     } else if (fromY == 1
  2573.            && board[fromY][fromX] == BlackPawn
  2574.            && toY == 0) {
  2575.     /* black pawn promotion */
  2576.     board[0][toX] = PromoPiece(*moveType);
  2577.     if (board[0][toX] == EmptySquare) {
  2578.         board[0][toX] = BlackQueen;
  2579.         *moveType = BlackPromotionQueen;
  2580.     }
  2581.     board[1][fromX] = EmptySquare;
  2582.     } else if ((fromY == 3)
  2583.            && (toX != fromX)
  2584.            && (board[fromY][fromX] == BlackPawn)
  2585.            && (board[toY][toX] == EmptySquare)) {
  2586.     *moveType = BlackCapturesEnPassant;
  2587.     board[fromY][fromX] = EmptySquare;
  2588.     board[toY][toX] = BlackPawn;
  2589.     board[toY + 1][toX] = EmptySquare;
  2590.     } else {
  2591.     *moveType = NormalMove;
  2592.     board[toY][toX] = board[fromY][fromX];
  2593.     board[fromY][fromX] = EmptySquare;
  2594.     }
  2595. }
  2596.  
  2597. /*
  2598.  * MakeMove() displays moves.  If they are illegal, GNU chess will detect
  2599.  * this and send an Illegal move message.  XBoard will then retract the move.
  2600.  *
  2601.  * Oddity: moveType is ignored on input unless the move is seen to be a
  2602.  * pawn promotion, in which case moveType tells us what to promote to.
  2603.  */
  2604. void MakeMove(moveType, fromX, fromY, toX, toY)
  2605.      ChessMove *moveType;
  2606.      int fromX, fromY, toX, toY;
  2607. {
  2608.     forwardMostMove++;
  2609.     if (commentList[forwardMostMove] != NULL) {
  2610.     free(commentList[forwardMostMove]);
  2611.     commentList[forwardMostMove] = NULL;
  2612.     }
  2613.     CopyBoard(boards[forwardMostMove], boards[forwardMostMove - 1]);
  2614.     ApplyMove(moveType, fromX, fromY, toX, toY,
  2615.           boards[forwardMostMove]);
  2616.     gameInfo.result = GameUnfinished;
  2617.     if (gameInfo.resultDetails != NULL) {
  2618.     free(gameInfo.resultDetails);
  2619.     gameInfo.resultDetails = NULL;
  2620.     }
  2621.     (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
  2622.                  FakeFlags(forwardMostMove - 1), EP_UNKNOWN,
  2623.                  fromY, fromX, toY, toX,
  2624.                  ToLower(PieceToChar(PromoPiece(*moveType))),
  2625.                  parseList[forwardMostMove - 1]);
  2626.  
  2627.     if (!pausing || gameMode == PlayFromGameFile) {
  2628.     currentMove = forwardMostMove;
  2629.     }
  2630.  
  2631.     SwitchClocks();
  2632.     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
  2633.     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
  2634.  
  2635.     if (gameMode == PlayFromGameFile ?
  2636.     matchMode || (appData.timeDelay == 0 && !pausing) : pausing) return;
  2637.  
  2638.     DisplayMove(currentMove - 1);
  2639.     DrawPosition(FALSE, boards[currentMove]);
  2640. }
  2641.  
  2642. void InitChessProgram(hostName, programName, pr, isr, sendTime)
  2643.      char *hostName, *programName;
  2644.      ProcRef *pr;
  2645.      InputSourceRef *isr;
  2646.      int *sendTime;
  2647. {
  2648.     char cmdLine[MSG_SIZ];
  2649.     char buf[MSG_SIZ];
  2650.     int err;
  2651.     
  2652.     if (appData.noChessProgram) return;
  2653.     
  2654.     if (*appData.searchTime != NULLCHAR) {
  2655.     sprintf(cmdLine, "%s %d", programName, searchTime);
  2656.     } else if (appData.searchDepth > 0) {
  2657.     sprintf(cmdLine, "%s 1 9999", programName);
  2658.     } else {
  2659.     sprintf(cmdLine, "%s %d %s", programName,
  2660.         appData.movesPerSession, appData.timeControl);
  2661.     }
  2662.  
  2663.     if (strcmp(hostName, "localhost") == 0) {
  2664.     err = StartChildProcess(cmdLine, pr);
  2665.     } else if (*appData.remoteShell == NULLCHAR) {
  2666.     err = OpenRcmd(hostName, appData.remoteUser, cmdLine, pr);
  2667.     } else {
  2668.     if (*appData.remoteUser == NULLCHAR) {
  2669.         sprintf(buf, "%s %s %s", appData.remoteShell, hostName, cmdLine);
  2670.     } else {
  2671.         sprintf(buf, "%s -l %s %s %s", appData.remoteShell,
  2672.             appData.remoteUser, hostName, cmdLine);
  2673.     }
  2674.     err = StartChildProcess(buf, pr);
  2675.     }
  2676.     
  2677.     if (err != 0) {
  2678.     sprintf(buf, "Startup failure on '%s'", programName);
  2679.     DisplayFatalError(buf, err, -1); /* don't exit */
  2680.     *pr = NoProc;
  2681.     *isr = NULL;
  2682.     return;
  2683.     }
  2684.     
  2685.     *isr = AddInputSource(*pr, TRUE, ReceiveFromProgram);
  2686.  
  2687.     hintRequested = FALSE;
  2688.     bookRequested = FALSE;
  2689.     maybePondering = FALSE;
  2690.     SendToProgram(appData.initString, *pr);
  2691.     SendSearchDepth(*pr);
  2692.     if (appData.showThinking) {
  2693.     SendToProgram("post\n", *pr);
  2694.     }
  2695.     
  2696.     if (*sendTime == 2) {
  2697.     /* Does program have "time" and "otim" commands? */
  2698.     char buf[MSG_SIZ];
  2699.     sprintf(timeTestStr, "%ld", timeControl/10);
  2700.     sprintf(buf, "time %s\notim %s\nhelp\n", timeTestStr, timeTestStr);
  2701.     /* "help" is a kludge to work around a gnuchess bug;
  2702.        some versions do not send a newline at the end of
  2703.        their response to the time command */
  2704.     SendToProgram(buf, *pr);
  2705.     }
  2706. }
  2707.  
  2708.  
  2709. void GameEnds(result, resultDetails, whosays)
  2710.      ChessMove result;
  2711.      char *resultDetails;
  2712.      int whosays;
  2713. {
  2714.     char *quit = "quit\n";
  2715.     int dummy;
  2716.  
  2717.     /* If we're loading the game from a file, stop */
  2718.     (void) StopLoadGameTimer();
  2719.     if (gameFileFP != NULL) {
  2720.     gameFileFP = NULL;
  2721.     }
  2722.  
  2723.     /* If this is an ICS game, only ICS can really say it's done;
  2724.        if not, anyone can. */
  2725.     if (!((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack
  2726.        || gameMode == IcsObserving || gameMode == IcsExamining)
  2727.       && whosays != GE_ICS)) {
  2728.     StopClocks();
  2729.     
  2730.     if (resultDetails != NULL) {
  2731.         gameInfo.result = result;
  2732.         gameInfo.resultDetails = StrSave(resultDetails);
  2733.  
  2734.         if (currentMove == forwardMostMove)
  2735.           DisplayMove(currentMove - 1);
  2736.     
  2737.         if (forwardMostMove != 0) {
  2738.         if (gameMode != PlayFromGameFile && gameMode != EditGame) {
  2739.             if (*appData.saveGameFile != NULLCHAR) {
  2740.             SaveGameToFile(appData.saveGameFile);
  2741.             } else if (appData.autoSaveGames) {
  2742.             AutoSaveGame();
  2743.             }
  2744.             if (*appData.savePositionFile != NULLCHAR) {
  2745.             SavePositionToFile(appData.savePositionFile);
  2746.             }
  2747.         }
  2748.         }
  2749.     }
  2750.  
  2751.     if (appData.icsActive) {
  2752.         if (appData.quietPlay &&
  2753.         (gameMode == IcsPlayingWhite ||
  2754.          gameMode == IcsPlayingBlack)) {
  2755.         SendToICS("set shout 1\n");
  2756.         }
  2757.         gameMode = IcsIdle;
  2758.         ics_user_moved = FALSE;
  2759.     } else if (gameMode != EditGame) {
  2760.         lastGameMode = gameMode;
  2761.         gameMode = EndOfGame;
  2762.     }
  2763.     pausing = FALSE;
  2764.     ModeHighlight();
  2765.     }
  2766.  
  2767.     if (appData.noChessProgram) return;
  2768.     if (result == GameUnfinished) return;
  2769.  
  2770.     /* Kill off chess programs */
  2771.     if (firstProgramISR != NULL)
  2772.       RemoveInputSource(firstProgramISR);
  2773.     firstProgramISR = NULL;
  2774.     
  2775.     if (firstProgramPR != NoProc) {
  2776.     InterruptChildProcess(firstProgramPR);
  2777.     OutputToProcess(firstProgramPR, quit, strlen(quit), &dummy);
  2778.     DestroyChildProcess(firstProgramPR);
  2779.     }
  2780.     firstProgramPR = NoProc;
  2781.     
  2782.     if (secondProgramISR != NULL)
  2783.       RemoveInputSource(secondProgramISR);
  2784.     secondProgramISR = NULL;
  2785.     
  2786.     if (secondProgramPR != NoProc) {
  2787.     InterruptChildProcess(secondProgramPR);
  2788.     OutputToProcess(secondProgramPR, quit, strlen(quit), &dummy);
  2789.     DestroyChildProcess(secondProgramPR);
  2790.     }
  2791.     secondProgramPR = NoProc;
  2792.     
  2793.     if (matchMode) {
  2794.     exit(0);
  2795.     }
  2796. }
  2797.  
  2798.  
  2799. void ResurrectChessProgram()
  2800.      /* The chess program may have exited.
  2801.         If so, restart it and feed it all the moves made so far. */
  2802. {
  2803.     int i;
  2804.     
  2805.     if (appData.noChessProgram || firstProgramPR != NoProc) return;
  2806.     
  2807.     InitChessProgram(appData.firstHost, appData.firstChessProgram,
  2808.              &firstProgramPR, &firstProgramISR, &firstSendTime);
  2809.     SendToProgram("force\n", firstProgramPR);
  2810.     
  2811.     if (startedFromSetupPosition) {
  2812.     if (backwardMostMove % 2 == 1)
  2813.       SendToProgram("a3\n", firstProgramPR);
  2814.     SendBoard(firstProgramPR, boards[backwardMostMove]);
  2815.     }
  2816.     
  2817.     for (i = backwardMostMove; i < currentMove; i++) {
  2818.     SendToProgram(moveList[i], firstProgramPR);
  2819.     }
  2820.     
  2821.     if (!firstSendTime) {
  2822.     /* can't tell gnuchess what its clock should read,
  2823.        so we bow to its notion. */
  2824.     ResetClocks();
  2825.     timeRemaining[0][currentMove] = whiteTimeRemaining;
  2826.     timeRemaining[1][currentMove] = blackTimeRemaining;
  2827.     }
  2828. }
  2829.  
  2830. /*
  2831.  * Button procedures
  2832.  */
  2833. void Reset(redraw)
  2834.      int redraw;
  2835. {
  2836.     int i;
  2837.  
  2838.     pausing = pauseExamInvalid = FALSE;
  2839.     flipView = appData.flipView;
  2840.     startedFromSetupPosition = blackPlaysFirst = FALSE;
  2841.     firstMove = TRUE;
  2842.     whiteFlag = blackFlag = FALSE;
  2843.     maybePondering = FALSE;
  2844.     thinkOutput[0] = NULLCHAR;
  2845.     ClearGameInfo(&gameInfo);
  2846.     ics_user_moved = ics_clock_paused = FALSE;
  2847.     ics_getting_history = H_FALSE;
  2848.     ics_gamenum = -1;
  2849.     
  2850.     ResetFrontEnd();
  2851.  
  2852.     GameEnds((ChessMove) 0, NULL, GE_PLAYER);
  2853.     lastGameMode = gameMode = BeginningOfGame;
  2854.     ModeHighlight();
  2855.     InitPosition(redraw);
  2856.     for (i = 0; i < MAX_MOVES; i++) {
  2857.     if (commentList[i] != NULL) {
  2858.         free(commentList[i]);
  2859.         commentList[i] = NULL;
  2860.     }
  2861.     }
  2862.     ResetClocks();
  2863.     timeRemaining[0][0] = whiteTimeRemaining;
  2864.     timeRemaining[1][0] = blackTimeRemaining;
  2865.     InitChessProgram(appData.firstHost, appData.firstChessProgram,
  2866.              &firstProgramPR, &firstProgramISR, &firstSendTime);
  2867.     DisplayTitle("");
  2868.     DisplayMessage("", "");
  2869. }
  2870.  
  2871.  
  2872. void LoadGameLoop()
  2873. {
  2874.     for (;;) {
  2875.     if (!LoadGameOneMove())
  2876.       return;
  2877.     if (matchMode || appData.timeDelay == 0)
  2878.       continue;
  2879.     if (appData.timeDelay < 0)
  2880.       return;
  2881.         StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
  2882.     break;
  2883.     }
  2884. }
  2885.  
  2886. int LoadGameOneMove()
  2887. {
  2888.     int fromX = 0, fromY = 0, toX = 0, toY = 0, done, editAfterDone;
  2889.     ChessMove moveType;
  2890.     char move[MSG_SIZ];
  2891.     char *p, *q;
  2892.     
  2893.     if (gameFileFP == NULL)
  2894.       return FALSE;
  2895.     
  2896.     if (gameMode != PlayFromGameFile) {
  2897.     gameFileFP = NULL;
  2898.     return FALSE;
  2899.     }
  2900.     
  2901.     yyboardindex = forwardMostMove;
  2902.     moveType = (ChessMove) yylex();
  2903.     
  2904.     done = FALSE;
  2905.     editAfterDone = TRUE;
  2906.     switch (moveType) {
  2907.       case Comment:
  2908.     if (appData.debugMode) 
  2909.       fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
  2910.     p = yy_text;
  2911.     if (*p == '{' || *p == '[' || *p == '(') {
  2912.         p[strlen(p) - 1] = NULLCHAR;
  2913.         p++;
  2914.     }
  2915.     while (*p == '\n') p++;
  2916.     if (!matchMode && (pausing || appData.timeDelay != 0)) {
  2917.         DisplayComment(currentMove - 1, p);
  2918.     }
  2919.     AppendComment(currentMove, p);
  2920.     return TRUE;
  2921.  
  2922.       case WhiteCapturesEnPassant:
  2923.       case BlackCapturesEnPassant:
  2924.       case WhitePromotionQueen:
  2925.       case BlackPromotionQueen:
  2926.       case WhitePromotionRook:
  2927.       case BlackPromotionRook:
  2928.       case WhitePromotionBishop:
  2929.       case BlackPromotionBishop:
  2930.       case WhitePromotionKnight:
  2931.       case BlackPromotionKnight:
  2932.       case NormalMove:
  2933.       case WhiteKingSideCastle:
  2934.       case WhiteQueenSideCastle:
  2935.       case BlackKingSideCastle:
  2936.       case BlackQueenSideCastle:
  2937.       case WhiteKingSideCastleWild:
  2938.       case WhiteQueenSideCastleWild:
  2939.       case BlackKingSideCastleWild:
  2940.       case BlackQueenSideCastleWild:
  2941.     if (appData.debugMode)
  2942.       fprintf(debugFP, "Parsed %s into %s\n",
  2943.           yy_text, currentMoveString);
  2944.     fromX = currentMoveString[0] - 'a';
  2945.     fromY = currentMoveString[1] - '1';
  2946.     toX = currentMoveString[2] - 'a';
  2947.     toY = currentMoveString[3] - '1';
  2948.     break;
  2949.  
  2950.       case WhiteWins:
  2951.       case BlackWins:
  2952.       case GameIsDrawn:
  2953.       case GameUnfinished:
  2954.     if (appData.debugMode)
  2955.       fprintf(debugFP, "Parsed game end: %s\n", yy_text);
  2956.     p = strchr(yy_text, '{');
  2957.     if (p == NULL) p = strchr(yy_text, '(');
  2958.     if (p == NULL) {
  2959.         p = yy_text;
  2960.         if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
  2961.     } else {
  2962.         q = strchr(p, *p == '{' ? '}' : ')');
  2963.         if (q != NULL) *q = NULLCHAR;
  2964.         p++;
  2965.     }
  2966.     GameEnds(moveType, p, GE_FILE);
  2967.     done = TRUE;
  2968.     editAfterDone = (moveType == GameUnfinished);
  2969.     if (cmailMsgLoaded) {
  2970.         flipView = WhiteOnMove(currentMove);
  2971.         if (moveType == GameUnfinished) flipView = !flipView;
  2972.         if (appData.debugMode)
  2973.           fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
  2974.     }
  2975.     break;
  2976.  
  2977.       case (ChessMove) 0:  /* end of file */
  2978.     if (appData.debugMode)
  2979.       fprintf(debugFP, "Parser hit end of file\n");
  2980.     switch (MateTest(boards[currentMove], FakeFlags(currentMove),
  2981.              EP_UNKNOWN)) {
  2982.       case MT_NONE:
  2983.       case MT_CHECK:
  2984.         DisplayMessage("", "End of game file");
  2985.         break;
  2986.       case MT_CHECKMATE:
  2987.         if (WhiteOnMove(currentMove)) {
  2988.         GameEnds(BlackWins, "Black mates", GE_FILE);
  2989.         } else {
  2990.         GameEnds(WhiteWins, "White mates", GE_FILE);
  2991.         }
  2992.         editAfterDone = FALSE;
  2993.         break;
  2994.       case MT_STALEMATE:
  2995.         GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
  2996.         editAfterDone = FALSE;
  2997.         break;
  2998.     }
  2999.     done = TRUE;
  3000.     break;
  3001.  
  3002.       case MoveNumberOne:
  3003.     if (lastLoadGameStart == GNUChessGame) {
  3004.         /* GNUChessGames have numbers, but they aren't move numbers */
  3005.         if (appData.debugMode)
  3006.           fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
  3007.               yy_text, (int) moveType);
  3008.         return LoadGameOneMove(); /* tail recursion */
  3009.     }
  3010.     /* else fall thru */
  3011.  
  3012.       case XBoardGame:
  3013.       case GNUChessGame:
  3014.       case PGNTag:
  3015.     /* Reached start of next game in file */
  3016.     if (appData.debugMode)
  3017.       fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
  3018.     switch (MateTest(boards[currentMove], FakeFlags(currentMove),
  3019.              EP_UNKNOWN)) {
  3020.       case MT_NONE:
  3021.       case MT_CHECK:
  3022.         DisplayMessage("", "End of game");
  3023.         break;
  3024.       case MT_CHECKMATE:
  3025.         if (WhiteOnMove(currentMove)) {
  3026.         GameEnds(BlackWins, "Black mates", GE_FILE);
  3027.         } else {
  3028.         GameEnds(WhiteWins, "White mates", GE_FILE);
  3029.         }
  3030.         editAfterDone = FALSE;
  3031.         break;
  3032.       case MT_STALEMATE:
  3033.         GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
  3034.         editAfterDone = FALSE;
  3035.         break;
  3036.     }
  3037.     done = TRUE;
  3038.     break;
  3039.  
  3040.       case PositionDiagram: /* should not happen; ignore */
  3041.       case ElapsedTime:    /* ignore */
  3042.     if (appData.debugMode)
  3043.       fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
  3044.           yy_text, (int) moveType);
  3045.     return LoadGameOneMove(); /* tail recursion */
  3046.  
  3047.       default:
  3048.       case BadMove:
  3049.     if (appData.debugMode)
  3050.       fprintf(debugFP, "Parsed BadMove: %s\n", yy_text);
  3051.     sprintf(move, "Bad move: %d. %s%s",
  3052.         (forwardMostMove / 2) + 1,
  3053.         WhiteOnMove(forwardMostMove) ? "" : "... ", yy_text);
  3054.     DisplayError(move, 0);
  3055.     done = TRUE;
  3056.     break;
  3057.  
  3058.       case AmbiguousMove:
  3059.     if (appData.debugMode)
  3060.       fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
  3061.     sprintf(move, "Ambiguous move: %d. %s%s",
  3062.         (forwardMostMove / 2) + 1,
  3063.         WhiteOnMove(forwardMostMove) ? "" : "... ", yy_text);
  3064.     DisplayError(move, 0);
  3065.     done = TRUE;
  3066.     break;
  3067.     }
  3068.     
  3069.     if (done) {
  3070.     if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
  3071.         DrawPosition(FALSE, boards[currentMove]);
  3072.         DisplayBothClocks();
  3073.         if (!appData.matchMode && commentList[currentMove] != NULL)
  3074.           DisplayComment(currentMove - 1, commentList[currentMove]);
  3075.     }
  3076.     if (editAfterDone) {
  3077.         lastGameMode = gameMode;
  3078.         gameMode = EditGame;
  3079.         ModeHighlight();
  3080.     }
  3081.         (void) StopLoadGameTimer();
  3082.     gameFileFP = NULL;
  3083.     cmailOldMove = forwardMostMove;
  3084.     return FALSE;
  3085.     } else {
  3086.     strcat(currentMoveString, "\n");
  3087.     strcpy(moveList[forwardMostMove], currentMoveString);
  3088.     SendToProgram(currentMoveString, firstProgramPR);
  3089.     
  3090.     thinkOutput[0] = NULLCHAR;
  3091.     MakeMove(&moveType, fromX, fromY, toX, toY);
  3092.     
  3093.     return TRUE;
  3094.     }
  3095. }
  3096.  
  3097. /* Load the nth game from the given file */
  3098. int LoadGameFromFile(filename, n, title, useList)
  3099.      char *filename;
  3100.      int n;
  3101.      char *title;
  3102.      /*Boolean*/ int useList;
  3103. {
  3104.     FILE *f;
  3105.     char buf[MSG_SIZ];
  3106.  
  3107.     if (strcmp(filename, "-") == 0) {
  3108.     f = stdin;
  3109.     title = "stdin";
  3110.     } else {
  3111.     f = fopen(filename, "r");
  3112.     if (f == NULL) {
  3113.         sprintf(buf, "Can't open \"%s\"", filename);
  3114.         DisplayError(buf, errno);
  3115.         return FALSE;
  3116.     }
  3117.     }
  3118.     if (fseek(f, 0, 0) == -1) {
  3119.     /* f is not seekable; probably a pipe */
  3120.     if (n == 0) n = 1;
  3121.     useList = FALSE;
  3122.     }
  3123.     if (useList && n == 0) {
  3124.     int error = GameListBuild(f);
  3125.     if (error) {
  3126.         DisplayError("Cannot build game list", error);
  3127.     } else if (!ListEmpty(&gameList) &&
  3128.            ((ListGame *) gameList.tailPred)->number > 1) {
  3129.         GameListPopUp(f, title);
  3130.         return TRUE;
  3131.     }
  3132.     GameListDestroy();
  3133.     n = 1;
  3134.     }
  3135.     return LoadGame(f, n, title, FALSE);
  3136. }
  3137.  
  3138.  
  3139. void MakeRegisteredMove()
  3140. {
  3141.     ChessMove moveType;
  3142.     
  3143.     if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
  3144.         switch (cmailMoveType[lastLoadGameNumber - 1]) {
  3145.           case CMAIL_MOVE:
  3146.           case CMAIL_DRAW:
  3147.         if (appData.debugMode)
  3148.           fprintf(debugFP, "Restoring %s for game %d\n",
  3149.               cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
  3150.     
  3151.         moveType =
  3152.           PromoCharToMoveType(WhiteOnMove(currentMove),
  3153.                   cmailMove[lastLoadGameNumber - 1][4]);
  3154.         thinkOutput[0] = NULLCHAR;
  3155.         strcpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1]);
  3156.         MakeMove(&moveType,
  3157.              cmailMove[lastLoadGameNumber - 1][0] - 'a',
  3158.              cmailMove[lastLoadGameNumber - 1][1] - '1',
  3159.              cmailMove[lastLoadGameNumber - 1][2] - 'a',
  3160.              cmailMove[lastLoadGameNumber - 1][3] - '1');
  3161.           
  3162.         switch (MateTest(boards[currentMove], FakeFlags(currentMove),
  3163.                  EP_UNKNOWN)) {
  3164.           case MT_NONE:
  3165.           case MT_CHECK:
  3166.         break;
  3167.             
  3168.           case MT_CHECKMATE:
  3169.         if (WhiteOnMove(currentMove)) {
  3170.             GameEnds(BlackWins, "Black mates", GE_PLAYER);
  3171.         } else {
  3172.             GameEnds(WhiteWins, "White mates", GE_PLAYER);
  3173.         }
  3174.         break;
  3175.             
  3176.           case MT_STALEMATE:
  3177.         GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
  3178.         break;
  3179.         }
  3180.  
  3181.         break;
  3182.         
  3183.       case CMAIL_RESIGN:
  3184.         if (WhiteOnMove(currentMove)) {
  3185.         GameEnds(BlackWins, "White resigns", GE_PLAYER);
  3186.         } else {
  3187.         GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
  3188.         }
  3189.         break;
  3190.         
  3191.       case CMAIL_ACCEPT:
  3192.         GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
  3193.         break;
  3194.           
  3195.       default:
  3196.         break;
  3197.     }
  3198.     }
  3199.  
  3200.     return;
  3201. }
  3202.  
  3203. /* Wrapper around LoadGame for use when a Cmail message is loaded */
  3204. int CmailLoadGame(f, gameNumber, title, useList)
  3205.      FILE *f;
  3206.      int gameNumber;
  3207.      char *title;
  3208.      int useList;
  3209. {
  3210.     int retVal;
  3211.  
  3212.     if (gameNumber > nCmailGames) {
  3213.     DisplayError("No more games in this message", 0);
  3214.     return FALSE;
  3215.     }
  3216.     if (f == lastLoadGameFP) {
  3217.     int offset = gameNumber - lastLoadGameNumber;
  3218.     if (offset == 0) {
  3219.         cmailMsg[0] = NULLCHAR;
  3220.         if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
  3221.         cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
  3222.         nCmailMovesRegistered--;
  3223.         }
  3224.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
  3225.         if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
  3226.         cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
  3227.         }
  3228.     } else {
  3229.         if (! RegisterMove()) return FALSE;
  3230.     }
  3231.     }
  3232.  
  3233.     retVal = LoadGame(f, gameNumber, title, useList);
  3234.  
  3235.     /* Make move registered during previous look at this game, if any */
  3236.     MakeRegisteredMove();
  3237.  
  3238.     if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
  3239.     commentList[currentMove]
  3240.       = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
  3241.     DisplayComment(currentMove - 1, commentList[currentMove]);
  3242.     }
  3243.  
  3244.     return retVal;
  3245. }
  3246.  
  3247. /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
  3248. int ReloadGame(offset)
  3249.      int offset;
  3250. {
  3251.     int gameNumber = lastLoadGameNumber + offset;
  3252.     if (lastLoadGameFP == NULL) {
  3253.     DisplayError("No game has been loaded yet", 0);
  3254.     return FALSE;
  3255.     }
  3256.     if (gameNumber <= 0) {
  3257.     DisplayError("Can't back up any further", 0);
  3258.     return FALSE;
  3259.     }
  3260.     if (cmailMsgLoaded) {
  3261.     return CmailLoadGame(lastLoadGameFP, gameNumber,
  3262.                  lastLoadGameTitle, lastLoadGameUseList);
  3263.     } else {
  3264.     return LoadGame(lastLoadGameFP, gameNumber,
  3265.             lastLoadGameTitle, lastLoadGameUseList);
  3266.     }
  3267. }
  3268.  
  3269.  
  3270. /* Load the nth game from open file f */
  3271. int LoadGame(f, gameNumber, title, useList)
  3272.      FILE *f;
  3273.      int gameNumber;
  3274.      char *title;
  3275.      int useList;
  3276. {
  3277.     ChessMove cm;
  3278.     char buf[MSG_SIZ];
  3279.     int gn = gameNumber;
  3280.  
  3281.     if (gameMode != BeginningOfGame) {
  3282.     Reset(FALSE);
  3283.     }
  3284.  
  3285.     gameFileFP = f;
  3286.     if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
  3287.     fclose(lastLoadGameFP);
  3288.     }
  3289.  
  3290.     if (useList) {
  3291.     ListGame *lg = (ListGame *) ListElem(&gameList, gameNumber-1);
  3292.     
  3293.     if (lg) {
  3294.         fseek(f, lg->offset, 0);
  3295.         GameListHighlight(gameNumber);
  3296.         gn = 1;
  3297.     }
  3298.     else {
  3299.         DisplayError("Game number out of range", 0);
  3300.         return FALSE;
  3301.     }
  3302.     } else {
  3303.     GameListDestroy();
  3304.     if (fseek(f, 0, 0) == -1) {
  3305.         if (f == lastLoadGameFP ?
  3306.         gameNumber == lastLoadGameNumber + 1 :
  3307.         gameNumber == 1) {
  3308.         gn = 1;
  3309.         } else {
  3310.         DisplayError("Can't seek on game file", 0);
  3311.         return FALSE;
  3312.         }
  3313.     }
  3314.     }
  3315.     lastLoadGameFP = f;
  3316.     lastLoadGameNumber = gameNumber;
  3317.  
  3318.     lastLoadGameUseList = useList;
  3319.  
  3320.     yynewfile(f);
  3321.  
  3322.     if (*title != NULLCHAR) {
  3323.     if (gameNumber > 1) {
  3324.         sprintf(buf, "%s %d", title, gameNumber);
  3325.         DisplayTitle(buf);
  3326.     } else {
  3327.         DisplayTitle(title);
  3328.     }
  3329.     }
  3330.     lastGameMode = gameMode = PlayFromGameFile;
  3331.     ModeHighlight();
  3332.     InitPosition(FALSE);
  3333.     StopClocks();
  3334.     if (firstProgramPR == NoProc) {
  3335.     InitChessProgram(appData.firstHost, appData.firstChessProgram,
  3336.              &firstProgramPR, &firstProgramISR, &firstSendTime);
  3337.     } else {
  3338.     SendToProgram(appData.initString, firstProgramPR);
  3339.     SendSearchDepth(firstProgramPR);
  3340.     }
  3341.     SendToProgram("force\n", firstProgramPR);
  3342.     
  3343.     /*
  3344.      * Skip the first gn-1 games in the file.
  3345.      * Also skip over anything that precedes an identifiable 
  3346.      * start of game marker, to avoid being confused by 
  3347.      * garbage at the start of the file.  Currently 
  3348.      * recognized start of game markers are the move number "1",
  3349.      * the pattern "gnuchess .* game", the pattern
  3350.      * "^[#;%] [^ ]* game file", and a PGN tag block.  
  3351.      * A game that starts with one of the latter two patterns
  3352.      * will also have a move number 1, possibly
  3353.      * following a position diagram.
  3354.      */
  3355.     cm = lastLoadGameStart = (ChessMove) 0;
  3356.     yyskipmoves = TRUE;
  3357.     while (gn > 0) {
  3358.     yyboardindex = forwardMostMove;
  3359.     cm = (ChessMove) yylex();
  3360.     yyskipmoves = FALSE;
  3361.     switch (cm) {
  3362.       case (ChessMove) 0:
  3363.         if (cmailMsgLoaded) {
  3364.         nCmailGames = CMAIL_MAX_GAMES - gn;
  3365.         } else {
  3366.         Reset(TRUE);
  3367.         DisplayError("Game not found in file", 0);
  3368.         }
  3369.         yyskipmoves = FALSE;
  3370.         return FALSE;
  3371.  
  3372.       case GNUChessGame:
  3373.       case XBoardGame:
  3374.         gn--;
  3375.         lastLoadGameStart = cm;
  3376.         break;
  3377.         
  3378.       case MoveNumberOne:
  3379.         switch (lastLoadGameStart) {
  3380.           case GNUChessGame:
  3381.           case XBoardGame:
  3382.           case PGNTag:
  3383.         break;
  3384.           case MoveNumberOne:
  3385.           case (ChessMove) 0:
  3386.         gn--;   /* count this game */
  3387.         lastLoadGameStart = cm;
  3388.         break;
  3389.           default:
  3390.         /* impossible */
  3391.         break;
  3392.         }
  3393.         break;
  3394.  
  3395.       case PGNTag:
  3396.         switch (lastLoadGameStart) {
  3397.           case GNUChessGame:
  3398.           case PGNTag:
  3399.           case MoveNumberOne:
  3400.           case (ChessMove) 0:
  3401.         gn--;   /* count this game */
  3402.         lastLoadGameStart = cm;
  3403.         break;
  3404.           case XBoardGame:
  3405.         lastLoadGameStart = cm;  /* game counted already */
  3406.         break;
  3407.           default:
  3408.         /* impossible */
  3409.         break;
  3410.         }
  3411.         if (gn > 0) {
  3412.         do {
  3413.             yyboardindex = forwardMostMove;
  3414.             cm = (ChessMove) yylex();
  3415.         } while (cm == PGNTag || cm == Comment);
  3416.         }
  3417.         break;
  3418.  
  3419.       case WhiteWins:
  3420.       case BlackWins:
  3421.       case GameIsDrawn:
  3422.          if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
  3423.         if (   cmailResult[CMAIL_MAX_GAMES - gn - 1]
  3424.             != CMAIL_OLD_RESULT) {
  3425.             nCmailResults ++ ;
  3426.             cmailResult[  CMAIL_MAX_GAMES
  3427.                 - gn - 1] = CMAIL_OLD_RESULT;
  3428.         }
  3429.         }
  3430.         break;
  3431.  
  3432.       default:
  3433.         break;
  3434.     }
  3435.     }
  3436.     yyskipmoves = FALSE;
  3437.     
  3438.     if (appData.debugMode)
  3439.       fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
  3440.  
  3441.     if (cm == XBoardGame) {
  3442.     /* Skip any header junk before position diagram and/or move 1 */
  3443.     for (;;) {
  3444.         yyboardindex = forwardMostMove;
  3445.         cm = (ChessMove) yylex();
  3446.  
  3447.         if (cm == (ChessMove) 0 ||
  3448.         cm == GNUChessGame || cm == XBoardGame) {
  3449.         /* Empty game; pretend end-of-file and handle later */
  3450.         cm = (ChessMove) 0;
  3451.         break;
  3452.         }
  3453.  
  3454.         if (cm == MoveNumberOne || cm == PositionDiagram ||
  3455.         cm == PGNTag || cm == Comment)
  3456.           break;
  3457.     }
  3458.     } else if (cm == GNUChessGame) {
  3459.     if (gameInfo.event != NULL) {
  3460.         free(gameInfo.event);
  3461.     }
  3462.     gameInfo.event = StrSave(yy_text);
  3463.     }    
  3464.  
  3465.     while (cm == PGNTag) {
  3466.     if (appData.debugMode) 
  3467.       fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
  3468.     ParsePGNTag(yy_text, &gameInfo);
  3469.     yyboardindex = forwardMostMove;
  3470.     cm = (ChessMove) yylex();
  3471.     /* Handle comments interspersed among the tags */
  3472.     while (cm == Comment) {
  3473.         char *p;
  3474.         if (appData.debugMode) 
  3475.           fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
  3476.         p = yy_text;
  3477.         if (*p == '{' || *p == '[' || *p == '(') {
  3478.         p[strlen(p) - 1] = NULLCHAR;
  3479.         p++;
  3480.         }
  3481.         while (*p == '\n') p++;
  3482.         AppendComment(currentMove, p);
  3483.         yyboardindex = forwardMostMove;
  3484.         cm = (ChessMove) yylex();
  3485.     }
  3486.     }
  3487.  
  3488.     if (gameInfo.event != NULL) {
  3489.     char *tags = PGNTags(&gameInfo);
  3490.     TagsPopUp(tags, CmailMsg());
  3491.     free(tags);
  3492.     } else {
  3493.     /* Make something up, but don't display it now */
  3494.     SetGameInfo();
  3495.     TagsPopDown();
  3496.     }
  3497.  
  3498.     if (cm == PositionDiagram) {
  3499.     int i, j;
  3500.     char *p;
  3501.     Board initial_position;
  3502.  
  3503.     if (appData.debugMode)
  3504.           fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
  3505.  
  3506.     if (gameInfo.fen == NULL) {
  3507.         p = yy_text;
  3508.         for (i = BOARD_SIZE - 1; i >= 0; i--)
  3509.           for (j = 0; j < BOARD_SIZE; p++)
  3510.         switch (*p) {
  3511.           case '[':
  3512.           case '-':
  3513.           case ' ':
  3514.           case '\t':
  3515.           case '\n':
  3516.           case '\r':
  3517.             break;
  3518.           default:
  3519.             initial_position[i][j++] = CharToPiece(*p);
  3520.             break;
  3521.         }
  3522.         while (*p == ' ' || *p == '\t' ||
  3523.            *p == '\n' || *p == '\r') p++;
  3524.     
  3525.         if (strncmp(p, "black", strlen("black"))==0)
  3526.           blackPlaysFirst = TRUE;
  3527.         else
  3528.           blackPlaysFirst = FALSE;
  3529.         startedFromSetupPosition = TRUE;
  3530.     
  3531.         if (blackPlaysFirst) {
  3532.         currentMove = forwardMostMove = backwardMostMove = 1;
  3533.         CopyBoard(boards[0], initial_position);
  3534.         CopyBoard(boards[1], initial_position);
  3535.         strcpy(moveList[0], "");
  3536.         strcpy(parseList[0], "");
  3537.         timeRemaining[0][1] = whiteTimeRemaining;
  3538.         timeRemaining[1][1] = blackTimeRemaining;
  3539.         SendToProgram("a3\n", firstProgramPR);
  3540.         SendCurrentBoard(firstProgramPR);
  3541.         if (commentList[0] != NULL) {
  3542.             commentList[1] = commentList[0];
  3543.             commentList[0] = NULL;
  3544.         }
  3545.         } else {
  3546.         currentMove = forwardMostMove = backwardMostMove = 0;
  3547.         CopyBoard(boards[0], initial_position);
  3548.         SendCurrentBoard(firstProgramPR);
  3549.         }
  3550.     }
  3551.     yyboardindex = forwardMostMove;
  3552.     cm = (ChessMove) yylex();
  3553.     }
  3554.  
  3555.     if (gameInfo.fen != NULL) {
  3556.     Board initial_position;
  3557.     startedFromSetupPosition = TRUE;
  3558.     if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
  3559.         Reset(TRUE);
  3560.         DisplayError("Bad FEN position in file", 0);
  3561.         return FALSE;
  3562.     }
  3563.     if (blackPlaysFirst) {
  3564.         currentMove = forwardMostMove = backwardMostMove = 1;
  3565.         CopyBoard(boards[0], initial_position);
  3566.         CopyBoard(boards[1], initial_position);
  3567.         strcpy(moveList[0], "");
  3568.         strcpy(parseList[0], "");
  3569.         timeRemaining[0][1] = whiteTimeRemaining;
  3570.         timeRemaining[1][1] = blackTimeRemaining;
  3571.         SendToProgram("a3\n", firstProgramPR);
  3572.         SendCurrentBoard(firstProgramPR);
  3573.         if (commentList[0] != NULL) {
  3574.         commentList[1] = commentList[0];
  3575.         commentList[0] = NULL;
  3576.         }
  3577.     } else {
  3578.         currentMove = forwardMostMove = backwardMostMove = 0;
  3579.         CopyBoard(boards[0], initial_position);
  3580.         SendCurrentBoard(firstProgramPR);
  3581.     }
  3582.     free(gameInfo.fen);
  3583.     gameInfo.fen = NULL;
  3584.     }
  3585.  
  3586.     while (cm == Comment) {
  3587.     char *p;
  3588.     if (appData.debugMode) 
  3589.       fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
  3590.     p = yy_text;
  3591.     if (*p == '{' || *p == '[' || *p == '(') {
  3592.         p[strlen(p) - 1] = NULLCHAR;
  3593.         p++;
  3594.     }
  3595.     while (*p == '\n') p++;
  3596.     AppendComment(currentMove, p);
  3597.     yyboardindex = forwardMostMove;
  3598.     cm = (ChessMove) yylex();
  3599.     }
  3600.  
  3601.     if (cm == (ChessMove) 0 || cm == WhiteWins || cm == BlackWins ||
  3602.     cm == GameIsDrawn || cm == GameUnfinished) {
  3603.     DisplayMessage("", "No moves in game");
  3604.     if (cmailMsgLoaded) {
  3605.         if (appData.debugMode)
  3606.           fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
  3607.         flipView = FALSE;
  3608.     }
  3609.     DrawPosition(FALSE, boards[currentMove]);
  3610.     DisplayBothClocks();
  3611.     lastGameMode = gameMode;
  3612.     gameMode = EditGame;
  3613.     ModeHighlight();
  3614.     gameFileFP = NULL;
  3615.     cmailOldMove = 0;
  3616.     return TRUE;
  3617.     }
  3618.  
  3619.     if (cm != MoveNumberOne && cm != GNUChessGame) {
  3620.     char buf[MSG_SIZ];
  3621.     Reset(TRUE);
  3622.     sprintf(buf, "Unexpected token in game: '%s' (%d)",
  3623.         yy_text, (int) cm);
  3624.     DisplayError(buf, 0);
  3625.     return FALSE;
  3626.     }
  3627.  
  3628.     if (commentList[currentMove] != NULL) {
  3629.     if (!matchMode && (pausing || appData.timeDelay != 0)) {
  3630.         DisplayComment(currentMove - 1, commentList[currentMove]);
  3631.     }
  3632.     if (!matchMode && appData.timeDelay != 0) {
  3633.         DrawPosition(FALSE, boards[currentMove]);
  3634.         if (appData.timeDelay > 0)
  3635.           StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
  3636.         return TRUE;
  3637.     }
  3638.     }
  3639.     if (!matchMode && appData.timeDelay != 0)
  3640.       DrawPosition(FALSE, boards[currentMove]);
  3641.  
  3642.     LoadGameLoop();
  3643.     
  3644.     return TRUE;
  3645. }
  3646.  
  3647. /* Load the nth position from the given file */
  3648. int LoadPositionFromFile(filename, n, title)
  3649.      char *filename;
  3650.      int n;
  3651.      char *title;
  3652. {
  3653.     FILE *f;
  3654.     char buf[MSG_SIZ];
  3655.  
  3656.     if (strcmp(filename, "-") == 0) {
  3657.     return LoadPosition(stdin, n, "stdin");
  3658.     } else {
  3659.     f = fopen(filename, "r");
  3660.     if (f == NULL) {
  3661.         sprintf(buf, "Can't open \"%s\"", filename);
  3662.         DisplayError(buf, errno);
  3663.         return FALSE;
  3664.     } else {
  3665.         return LoadPosition(f, n, title);
  3666.     }
  3667.     }
  3668. }
  3669.  
  3670. /* Load the nth position from the given open file, and close it */
  3671. int LoadPosition(fp, positionNumber, title)
  3672.      FILE *fp;
  3673.      int positionNumber;
  3674.      char *title;
  3675. {
  3676.     char *p, line[MSG_SIZ];
  3677.     Board initial_position;
  3678.     int i, j, fenMode;
  3679.     
  3680.     if (gameMode != BeginningOfGame) {
  3681.     Reset(TRUE);
  3682.     }
  3683.  
  3684.     if (positionNumber > 1) {
  3685.     sprintf(line, "%s %d", title, positionNumber);
  3686.     DisplayTitle(line);
  3687.     } else {
  3688.     DisplayTitle(title);
  3689.     }
  3690.     lastGameMode = gameMode = EditGame;
  3691.     ModeHighlight();
  3692.     startedFromSetupPosition = TRUE;
  3693.     
  3694.     if (firstProgramPR == NoProc)
  3695.       InitChessProgram(appData.firstHost, appData.firstChessProgram,
  3696.                &firstProgramPR, &firstProgramISR, &firstSendTime);
  3697.     
  3698.     /* Negative position number means to seek to that byte offset */
  3699.     if (positionNumber < 0) {
  3700.     fseek(fp, -positionNumber, 0);
  3701.     positionNumber = 1;
  3702.     }
  3703.     /* See if this file is FEN or old-style xboard */
  3704.     if (fgets(line, MSG_SIZ, fp) == NULL) {
  3705.     Reset(TRUE);
  3706.     DisplayError("Position not found in file", 0);
  3707.     return FALSE;
  3708.     }
  3709.     switch (line[0]) {
  3710.       case '#':  case 'x':
  3711.       default:
  3712.     fenMode = FALSE;
  3713.     break;
  3714.       case 'p':  case 'n':  case 'b':  case 'r':  case 'q':  case 'k':
  3715.       case 'P':  case 'N':  case 'B':  case 'R':  case 'Q':  case 'K':
  3716.       case '1':  case '2':  case '3':  case '4':  case '5':  case '6':
  3717.       case '7':  case '8':
  3718.     fenMode = TRUE;
  3719.     break;
  3720.     }
  3721.  
  3722.     if (positionNumber >= 2  /* don't look for '#' if positionNumber == 1 */) {
  3723.     if (fenMode || line[0] == '#') positionNumber--;
  3724.     while (positionNumber > 0) {
  3725.         /* skip postions before number positionNumber */
  3726.         if (fgets(line, MSG_SIZ, fp) == NULL) {
  3727.         Reset(TRUE);
  3728.         DisplayError("Position not found in file", 0);
  3729.         return FALSE;
  3730.         }
  3731.         if (fenMode || line[0] == '#') positionNumber--;
  3732.     }
  3733.     }
  3734.  
  3735.     if (fenMode) {
  3736.     if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
  3737.         Reset(TRUE);
  3738.         DisplayError("Bad FEN position in file", 0);
  3739.         return FALSE;
  3740.     }
  3741.     } else {
  3742.     (void) fgets(line, MSG_SIZ, fp);
  3743.     (void) fgets(line, MSG_SIZ, fp);
  3744.     
  3745.     for (i = BOARD_SIZE - 1; i >= 0; i--) {
  3746.         (void) fgets(line, MSG_SIZ, fp);
  3747.         for (p = line, j = 0; j < BOARD_SIZE; p++) {
  3748.         if (*p == ' ')
  3749.           continue;
  3750.         initial_position[i][j++] = CharToPiece(*p);
  3751.         }
  3752.     }
  3753.     
  3754.     blackPlaysFirst = FALSE;
  3755.     if (!feof(fp)) {
  3756.         (void) fgets(line, MSG_SIZ, fp);
  3757.         if (strncmp(line, "black", strlen("black"))==0)
  3758.           blackPlaysFirst = TRUE;
  3759.     }
  3760.     }
  3761.     fclose(fp);
  3762.     
  3763.     if (blackPlaysFirst) {
  3764.     CopyBoard(boards[0], initial_position);
  3765.     strcpy(moveList[0], "");
  3766.     strcpy(parseList[0], "");
  3767.     currentMove = forwardMostMove = backwardMostMove = 1;
  3768.     CopyBoard(boards[1], initial_position);
  3769.     SendToProgram("force\na3\n", firstProgramPR);
  3770.     SendCurrentBoard(firstProgramPR);
  3771.     DisplayMessage("", "Black to play");
  3772.     } else {
  3773.     currentMove = forwardMostMove = backwardMostMove = 0;
  3774.     CopyBoard(boards[0], initial_position);
  3775.     SendCurrentBoard(firstProgramPR);
  3776.     SendToProgram("force\n", firstProgramPR);
  3777.     DisplayMessage("", "White to play");
  3778.     }
  3779.     
  3780.     ResetClocks();
  3781.     timeRemaining[0][1] = whiteTimeRemaining;
  3782.     timeRemaining[1][1] = blackTimeRemaining;
  3783.  
  3784.     DrawPosition(FALSE, boards[currentMove]);
  3785.     
  3786.     return TRUE;
  3787. }
  3788.  
  3789.  
  3790. void CopyPlayerNameIntoFileName(dest, src)
  3791.      char **dest, *src;
  3792. {
  3793.     while (*src != NULLCHAR && *src != ',') {
  3794.     if (*src == ' ') {
  3795.         *(*dest)++ = '_';
  3796.         src++;
  3797.     } else {
  3798.         *(*dest)++ = *src++;
  3799.     }
  3800.     }
  3801. }
  3802.  
  3803. char *DefaultFileName(ext)
  3804.      char *ext;
  3805. {
  3806.     static char def[MSG_SIZ];
  3807.     char *p;
  3808.  
  3809.     if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
  3810.     p = def;
  3811.     CopyPlayerNameIntoFileName(&p, gameInfo.white);
  3812.     *p++ = '-';
  3813.     CopyPlayerNameIntoFileName(&p, gameInfo.black);
  3814.     *p++ = '.';
  3815.     strcpy(p, ext);
  3816.     } else {
  3817.     def[0] = NULLCHAR;
  3818.     }
  3819.     return def;
  3820. }
  3821.  
  3822. /* Save the current game to the given file */
  3823. int SaveGameToFile(filename)
  3824.      char *filename;
  3825. {
  3826.     FILE *f;
  3827.     char buf[MSG_SIZ];
  3828.  
  3829.     if (strcmp(filename, "-") == 0) {
  3830.     return SaveGame(toUserFP, 0, NULL);
  3831.     } else {
  3832.     f = fopen(filename, "a");
  3833.     if (f == NULL) {
  3834.         sprintf(buf, "Can't open \"%s\"", filename);
  3835.         DisplayError(buf, errno);
  3836.         return FALSE;
  3837.     } else {
  3838.         return SaveGame(f, 0, NULL);
  3839.     }
  3840.     }
  3841. }
  3842.  
  3843. char *SavePart(str)
  3844.      char *str;
  3845. {
  3846.     static char buf[MSG_SIZ];
  3847.     char *p;
  3848.     
  3849.     p = strchr(str, ' ');
  3850.     if (p == NULL) return str;
  3851.     strncpy(buf, str, p - str);
  3852.     buf[p - str] = NULLCHAR;
  3853.     return buf;
  3854. }
  3855.  
  3856. #define PGN_MAX_LINE 75
  3857.  
  3858. int SaveGamePGN(f)
  3859.      FILE *f;
  3860. {
  3861.     int i, offset, linelen, newblock;
  3862.     time_t tm;
  3863.     char *movetext;
  3864.     char numtext[32];
  3865.     int movelen, numlen, blank;
  3866.     
  3867.     tm = time((time_t *) NULL);
  3868.     
  3869.     PrintPGNTags(f, &gameInfo);
  3870.     
  3871.     if (backwardMostMove > 0 || startedFromSetupPosition) {
  3872.     fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n",
  3873.         PositionToFEN(backwardMostMove));
  3874.     fprintf(f, "\n{--------------\n");
  3875.     PrintPosition(f, backwardMostMove);
  3876.     fprintf(f, "--------------}\n");
  3877.     } else {
  3878.     fprintf(f, "\n");
  3879.     }
  3880.  
  3881.     i = backwardMostMove;
  3882.     offset = backwardMostMove & (~1L);  /* output move numbers start at 1 */
  3883.     linelen = 0;
  3884.     newblock = TRUE;
  3885.  
  3886.     while (i < forwardMostMove) {
  3887.     /* Print comments preceding this move */
  3888.     if (commentList[i] != NULL) {
  3889.         if (linelen > 0) fprintf(f, "\n");
  3890.         fprintf(f, "{\n%s}\n", commentList[i]);
  3891.         linelen = 0;
  3892.         newblock = TRUE;
  3893.     }
  3894.  
  3895.     /* Format move number */
  3896.         if ((i % 2) == 0) {
  3897.         sprintf(numtext, "%d.", (i - offset)/2 + 1);
  3898.     } else {
  3899.         if (newblock) {
  3900.         sprintf(numtext, "%d...", (i - offset)/2 + 1);
  3901.         } else {
  3902.         numtext[0] = NULLCHAR;
  3903.         }
  3904.     }
  3905.     numlen = strlen(numtext);
  3906.     newblock = FALSE;
  3907.  
  3908.     /* Print move number */
  3909.     blank = linelen > 0 && numlen > 0;
  3910.     if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
  3911.         fprintf(f, "\n");
  3912.         linelen = 0;
  3913.         blank = 0;
  3914.     }
  3915.     if (blank) {
  3916.         fprintf(f, " ");
  3917.         linelen++;
  3918.     }
  3919.     fprintf(f, numtext);
  3920.     linelen += numlen;
  3921.  
  3922.     /* Get move */
  3923.         movetext = SavePart(parseList[i]);
  3924.     movelen = strlen(movetext);
  3925.  
  3926.     /* Print move */
  3927.     blank = linelen > 0 && movelen > 0;
  3928.     if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
  3929.         fprintf(f, "\n");
  3930.         linelen = 0;
  3931.         blank = 0;
  3932.     }
  3933.     if (blank) {
  3934.         fprintf(f, " ");
  3935.         linelen++;
  3936.     }
  3937.     fprintf(f, movetext);
  3938.     linelen += movelen;
  3939.  
  3940.     i++;
  3941.     }
  3942.     
  3943.     /* Start a new line */
  3944.     if (linelen > 0) fprintf(f, "\n");
  3945.  
  3946.     /* Print comments after last move */
  3947.     if (commentList[i] != NULL) {
  3948.     fprintf(f, "{\n%s}\n", commentList[i]);
  3949.     }
  3950.  
  3951.     /* Print result */
  3952.     if (gameInfo.resultDetails != NULL &&
  3953.     gameInfo.resultDetails[0] != NULLCHAR) {
  3954.     fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
  3955.         PGNResult(gameInfo.result));
  3956.     } else {
  3957.     fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
  3958.     }
  3959.  
  3960.     fclose(f);
  3961.     return TRUE;
  3962. }
  3963.  
  3964. int SaveGameOldStyle(f)
  3965.      FILE *f;
  3966. {
  3967.     int i, offset;
  3968.     time_t tm;
  3969.     extern char *programName;
  3970.     
  3971.     tm = time((time_t *) NULL);
  3972.     
  3973.     fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
  3974.     PrintOpponents(f);
  3975.     
  3976.     if (backwardMostMove > 0 || startedFromSetupPosition) {
  3977.     fprintf(f, "\n[--------------\n");
  3978.     PrintPosition(f, backwardMostMove);
  3979.     fprintf(f, "--------------]\n");
  3980.     } else {
  3981.     fprintf(f, "\n");
  3982.     }
  3983.  
  3984.     i = backwardMostMove;
  3985.     offset = backwardMostMove & (~1L);  /* output move numbers start at 1 */
  3986.  
  3987.     while (i < forwardMostMove) {
  3988.     if (commentList[i] != NULL) {
  3989.         fprintf(f, "[%s]\n", commentList[i]);
  3990.     }
  3991.  
  3992.     if ((i % 2) == 1) {
  3993.         fprintf(f, "%d. ...  %s\n", (i - offset)/2 + 1, parseList[i]);
  3994.         i++;
  3995.     } else {
  3996.         fprintf(f, "%d. %s  ", (i - offset)/2 + 1, parseList[i]);
  3997.         i++;
  3998.         if (commentList[i] != NULL) {
  3999.         fprintf(f, "\n");
  4000.         continue;
  4001.         }
  4002.         if (i >= forwardMostMove) {
  4003.         fprintf(f, "\n");
  4004.         break;
  4005.         }
  4006.         fprintf(f, "%s\n", parseList[i]);
  4007.         i++;
  4008.     }
  4009.     }
  4010.     
  4011.     if (commentList[i] != NULL) {
  4012.     fprintf(f, "[%s]\n", commentList[i]);
  4013.     }
  4014.  
  4015.     /* This isn't really the old style, but it's close enough */
  4016.     if (gameInfo.resultDetails != NULL &&
  4017.     gameInfo.resultDetails[0] != NULLCHAR) {
  4018.     fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
  4019.         gameInfo.resultDetails);
  4020.     } else {
  4021.     fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
  4022.     }
  4023.  
  4024.     fclose(f);
  4025.     return TRUE;
  4026. }
  4027.  
  4028. /* Save the current game to open file f and close the file */
  4029. int SaveGame(f, dummy, dummy2)
  4030.      FILE *f;
  4031.      int dummy;
  4032.      char *dummy2;
  4033. {
  4034.     if (appData.oldSaveStyle)
  4035.       return SaveGameOldStyle(f);
  4036.     else
  4037.       return SaveGamePGN(f);
  4038. }
  4039.  
  4040. /* Save the current position to the given file */
  4041. int SavePositionToFile(filename)
  4042.      char *filename;
  4043. {
  4044.     FILE *f;
  4045.     char buf[MSG_SIZ];
  4046.  
  4047.     if (strcmp(filename, "-") == 0) {
  4048.     return SavePosition(toUserFP, 0, NULL);
  4049.     } else {
  4050.     f = fopen(filename, "a");
  4051.     if (f == NULL) {
  4052.         sprintf(buf, "Can't open \"%s\"", filename);
  4053.         DisplayError(buf, errno);
  4054.         return FALSE;
  4055.     } else {
  4056.         SavePosition(f, 0, NULL);
  4057.         return TRUE;
  4058.     }
  4059.     }
  4060. }
  4061.  
  4062. /* Save the current position to the given open file and close the file */
  4063. int SavePosition(f, dummy, dummy2)
  4064.      FILE *f;
  4065.      int dummy;
  4066.      char *dummy2;
  4067. {
  4068.     time_t tm;
  4069.     extern char *programName;
  4070.     char *fen;
  4071.     
  4072.     if (appData.oldSaveStyle) {
  4073.     tm = time((time_t *) NULL);
  4074.     
  4075.     fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
  4076.     PrintOpponents(f);
  4077.     fprintf(f, "[--------------\n");
  4078.     PrintPosition(f, currentMove);
  4079.     fprintf(f, "--------------]\n");
  4080.     } else {
  4081.     fen = PositionToFEN(currentMove);
  4082.     fprintf(f, "%s\n", fen);
  4083.     free(fen);
  4084.     }
  4085.     fclose(f);
  4086.     return TRUE;
  4087. }
  4088.  
  4089. void ReloadCmailMsgEvent(unregister)
  4090.      int unregister;
  4091. {
  4092.     static char *inFilename = NULL;
  4093.     static char *outFilename;
  4094.     int i;
  4095.     struct stat inbuf, outbuf;
  4096.     int status;
  4097.     
  4098.     /* Any registered moves are unregistered if unregister is set, */
  4099.     /* i.e. invoked by the signal handler */
  4100.     if (unregister) {
  4101.     for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
  4102.         cmailMoveRegistered[i] = FALSE;
  4103.         if (cmailCommentList[i] != NULL) {
  4104.         free(cmailCommentList[i]);
  4105.         cmailCommentList[i] = NULL;
  4106.         }
  4107.     }
  4108.     nCmailMovesRegistered = 0;
  4109.     }
  4110.  
  4111.     Reset(TRUE);
  4112.     
  4113.     for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
  4114.     cmailResult[i] = CMAIL_NOT_RESULT;
  4115.     }
  4116.     nCmailResults = 0;
  4117.  
  4118.     if (inFilename == NULL) {
  4119.     /* Because the filenames are static they only get malloced once  */
  4120.     /* and they never get freed                                      */
  4121.     inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
  4122.     sprintf(inFilename, "%s.game.in", appData.cmailGameName);
  4123.  
  4124.     outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
  4125.     sprintf(outFilename, "%s.out", appData.cmailGameName);
  4126.     }
  4127.     
  4128.     status = stat(outFilename, &outbuf);
  4129.     if (status < 0) {
  4130.     cmailMailedMove = FALSE;
  4131.     } else {
  4132.     status = stat(inFilename, &inbuf);
  4133.     cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
  4134.     }
  4135.     
  4136.     /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
  4137.        counts the games, notes how each one terminated, etc.
  4138.  
  4139.        It would be nice to remove this kludge and instead gather all
  4140.        the information while building the game list.  (And to keep it
  4141.        in the game list nodes instead of having a bunch of fixed-size
  4142.        parallel arrays.)  Note this will require getting each game's
  4143.        termination from the PGN tags, as the game list builder does
  4144.        not process the game moves.  --mann
  4145.     */
  4146.     cmailMsgLoaded = TRUE;
  4147.     LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
  4148.     
  4149.     /* Load first game in the file or popup game menu */
  4150.     LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
  4151.  
  4152.     return;
  4153. }
  4154.  
  4155. int RegisterMove()
  4156. {
  4157.     FILE *f;
  4158.     char string[MSG_SIZ];
  4159.  
  4160.     if (   cmailMailedMove
  4161.     || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
  4162.     return TRUE;                  /* Allow free viewing  */
  4163.     }
  4164.  
  4165.     /* Unregister move to ensure that we don't leave RegisterMove        */
  4166.     /* with the move registered when the conditions for registering no   */
  4167.     /* longer hold                                                       */
  4168.     if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
  4169.     cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
  4170.     nCmailMovesRegistered --;
  4171.  
  4172.     if (cmailCommentList[lastLoadGameNumber - 1] != NULL) 
  4173.     {
  4174.         free(cmailCommentList[lastLoadGameNumber - 1]);
  4175.         cmailCommentList[lastLoadGameNumber - 1] = NULL;
  4176.     }
  4177.     }
  4178.  
  4179.     if (cmailOldMove == -1) {
  4180.     DisplayError("You have edited the game history.\nUse Reload Same Game and make your move again.", 0);
  4181.     return FALSE;
  4182.     }
  4183.  
  4184.     if (currentMove > cmailOldMove + 1) {
  4185.     DisplayError("You have entered too many moves.\nBack up to the correct position and try again.", 0);
  4186.     return FALSE;
  4187.     }
  4188.  
  4189.     if (currentMove < cmailOldMove) {
  4190.         DisplayError("Displayed position is not current.\nStep forward to the correct position and try again.", 0);
  4191.         return FALSE;
  4192.     }
  4193.  
  4194.     if (forwardMostMove > currentMove) {
  4195.     /* Silently truncate extra moves */
  4196.     TruncateGame();
  4197.     }
  4198.  
  4199.     if (   (currentMove == cmailOldMove + 1)
  4200.     || (   (currentMove == cmailOldMove)
  4201.         && (   (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
  4202.         || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
  4203.     if (gameInfo.result != GameUnfinished) {
  4204.         cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
  4205.     }
  4206.  
  4207.     if (commentList[currentMove] != NULL) {
  4208.         cmailCommentList[lastLoadGameNumber - 1]
  4209.           = StrSave(commentList[currentMove]);
  4210.     }
  4211.     strcpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1]);
  4212.  
  4213.     if (appData.debugMode)
  4214.       fprintf(debugFP, "Saving %s for game %d\n",
  4215.           cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
  4216.  
  4217.     sprintf(string,
  4218.         "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
  4219.     
  4220.     f = fopen(string, "w");
  4221.     if (appData.oldSaveStyle) {
  4222.         SaveGameOldStyle(f);
  4223.         fclose(f);
  4224.         
  4225.         sprintf(string, "%s.pos.out", appData.cmailGameName);
  4226.         f = fopen(string, "w");
  4227.         SavePosition(f, 0, NULL);
  4228.         fclose(f);
  4229.     } else {
  4230.         fprintf(f, "{--------------\n");
  4231.         PrintPosition(f, currentMove);
  4232.         fprintf(f, "--------------}\n\n");
  4233.         
  4234.         SaveGame(f, 0, NULL);
  4235.         fclose(f);
  4236.     }
  4237.     
  4238.     cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
  4239.     nCmailMovesRegistered ++;
  4240.     } else if (nCmailGames == 1) {
  4241.     DisplayError("You have not made a move yet.", 0);
  4242.     return FALSE;
  4243.     }
  4244.  
  4245.     return TRUE;
  4246. }
  4247.     
  4248. void MailMoveEvent()
  4249. {
  4250.     static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
  4251.     FILE *commandOutput;
  4252.     char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
  4253.     int nBytes = 0; /*  Suppress warnings on uninitialized variables    */
  4254.     int nBuffers;
  4255.     int i;
  4256.     int archived;
  4257.     char *arcDir;
  4258.  
  4259.     if (! cmailMsgLoaded) {
  4260.     DisplayError("The cmail message is not loaded.\nUse Reload CMail Message and make your move again.", 0);
  4261.     return;
  4262.     }
  4263.  
  4264.     if (nCmailGames == nCmailResults) {
  4265.         DisplayError("No unfinished games", 0);
  4266.     return;
  4267.     }
  4268.  
  4269. #ifdef CMAIL_PROHIBIT_REMAIL
  4270.     if (cmailMailedMove) {
  4271.     sprintf(msg, "You have already mailed a move.\nWait until a move arrives from your opponent.\nTo resend the same move, type\n\"cmail -remail -game %s\"\non the command line.", appData.cmailGameName);
  4272.         DisplayError(msg, 0);
  4273.     return;
  4274.     }
  4275. #endif
  4276.  
  4277.     if (! (cmailMailedMove || RegisterMove())) return;
  4278.     
  4279.     if (   cmailMailedMove
  4280.     || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
  4281.     sprintf(string, partCommandString,
  4282.         appData.debugMode ? " -v" : "", appData.cmailGameName);
  4283.     commandOutput = popen(string, "r");
  4284.  
  4285.     if (commandOutput == NULL) {
  4286.         DisplayError("Failed to invoke cmail", 0);
  4287.     } else {
  4288.         for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
  4289.         nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
  4290.         }
  4291.         if (nBuffers > 1) {
  4292.         (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
  4293.         (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
  4294.         nBytes = MSG_SIZ - 1;
  4295.         } else {
  4296.         (void) memcpy(msg, buffer, nBytes);
  4297.         }
  4298.         *(msg + nBytes) = '\0';          /* \0 for end-of-string*/
  4299.  
  4300.         if(StrStr(msg, "Mailed cmail message to ") != NULL) {
  4301.         cmailMailedMove = TRUE;          /* Prevent >1 moves    */
  4302.  
  4303.         archived = TRUE;
  4304.         for (i = 0; i < nCmailGames; i ++) {
  4305.             if (cmailResult[i] == CMAIL_NOT_RESULT) {
  4306.             archived = FALSE;
  4307.             }
  4308.         }
  4309.         if (   archived
  4310.             && (   (arcDir = (char *) getenv("CMAIL_ARCDIR"))
  4311.                 != NULL)) {
  4312.             sprintf(buffer, "%s/%s.%s.archive",
  4313.                 arcDir,
  4314.                 appData.cmailGameName,
  4315.                 gameInfo.date);
  4316.             LoadGameFromFile(buffer, 1, buffer, FALSE);
  4317.             cmailMsgLoaded = FALSE;
  4318.         }
  4319.         }
  4320.  
  4321.         DisplayInformation(msg);
  4322.         pclose(commandOutput);
  4323.     }
  4324.     } else {
  4325.     if ((*cmailMsg) != '\0') {
  4326.         DisplayInformation(cmailMsg);
  4327.     }
  4328.     }
  4329.  
  4330.     return;
  4331. }
  4332.  
  4333. char *CmailMsg()
  4334. {
  4335.     int  prependComma = 0;
  4336.     char number[5];
  4337.     char string[MSG_SIZ];  /* Space for game-list */
  4338.     int  i;
  4339.     
  4340.     if (!cmailMsgLoaded) return "";
  4341.  
  4342.     if (cmailMailedMove) {
  4343.     sprintf(cmailMsg, "Waiting for reply from opponent\n");
  4344.     } else {
  4345.     /* Create a list of games left */
  4346.     sprintf(string, "[");
  4347.     for (i = 0; i < nCmailGames; i ++) {
  4348.         if (! (   cmailMoveRegistered[i]
  4349.            || (cmailResult[i] == CMAIL_OLD_RESULT))) {
  4350.         if (prependComma) {
  4351.             sprintf(number, ",%d", i + 1);
  4352.         } else {
  4353.             sprintf(number, "%d", i + 1);
  4354.             prependComma = 1;
  4355.         }
  4356.         
  4357.         strcat(string, number);
  4358.         }
  4359.     }
  4360.     strcat(string, "]");
  4361.  
  4362.     if (nCmailMovesRegistered + nCmailResults == 0) {
  4363.         switch (nCmailGames) {
  4364.           case 1:
  4365.         sprintf(cmailMsg,
  4366.             "Still need to make move for game\n");
  4367.         break;
  4368.         
  4369.           case 2:
  4370.         sprintf(cmailMsg,
  4371.             "Still need to make moves for both games\n");
  4372.         break;
  4373.         
  4374.           default:
  4375.         sprintf(cmailMsg,
  4376.             "Still need to make moves for all %d games\n",
  4377.             nCmailGames);
  4378.         break;
  4379.         }
  4380.     } else {
  4381.         switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
  4382.           case 1:
  4383.         sprintf(cmailMsg,
  4384.             "Still need to make a move for game %s\n",
  4385.             string);
  4386.         break;
  4387.         
  4388.           case 0:
  4389.         if (nCmailResults == nCmailGames) {
  4390.             sprintf(cmailMsg, "No unfinished games\n");
  4391.         } else {
  4392.             sprintf(cmailMsg, "Ready to send mail\n");
  4393.         }
  4394.         break;
  4395.         
  4396.           default:
  4397.         sprintf(cmailMsg,
  4398.             "Still need to make moves for games %s\n",
  4399.             string);
  4400.         }
  4401.     }
  4402.     }
  4403.  
  4404.     return cmailMsg;
  4405. }
  4406.  
  4407. void ResetGameEvent()
  4408. {
  4409.     Reset(TRUE);
  4410.     cmailMsgLoaded = FALSE;
  4411.     if (appData.icsActive)
  4412.       SendToICS("refresh\n");
  4413. }
  4414.  
  4415. static int exitrecur = 0;
  4416.  
  4417. void ExitEvent(status)
  4418.      int status;
  4419. {
  4420.     if (exitrecur++ > 0) exit(status);  /* error during exit processing */
  4421.  
  4422.     if (icsPR != NoProc) {
  4423.     DestroyChildProcess(icsPR);
  4424.     }
  4425.     /* Save game if resource set and not already saved by GameEnds() */
  4426.     if (gameInfo.resultDetails == NULL && forwardMostMove > 0) {
  4427.     if (*appData.saveGameFile != NULLCHAR) {
  4428.         SaveGameToFile(appData.saveGameFile);
  4429.     } else if (appData.autoSaveGames) {
  4430.         AutoSaveGame();
  4431.     }
  4432.     if (*appData.savePositionFile != NULLCHAR) {
  4433.         SavePositionToFile(appData.savePositionFile);
  4434.     }
  4435.     }
  4436.     GameEnds((ChessMove) 0, NULL, GE_PLAYER);
  4437.  
  4438.     exit(status);
  4439. }
  4440.  
  4441. void PauseEvent()
  4442. {
  4443.     if (pausing) {
  4444.     pausing = FALSE;
  4445.     ModeHighlight();
  4446.     if (gameMode == MachinePlaysWhite ||
  4447.         gameMode == MachinePlaysBlack) {
  4448.         StartClocks();
  4449.     } else {
  4450.         DisplayBothClocks();
  4451.     }
  4452.     if (gameMode == PlayFromGameFile) {
  4453.         if (appData.timeDelay >= 0) LoadGameLoop();
  4454.     } else if (gameMode == IcsExamining && pauseExamInvalid) {
  4455.         Reset(FALSE);
  4456.         SendToICS("refresh\n");
  4457.     } else if (currentMove < forwardMostMove) {
  4458.         ForwardInner(forwardMostMove);
  4459.     }
  4460.     pauseExamInvalid = FALSE;
  4461.     } else {
  4462.     switch (gameMode) {
  4463.       default:
  4464.         return;
  4465.       case IcsExamining:
  4466.         pauseExamForwardMostMove = forwardMostMove;
  4467.         pauseExamInvalid = FALSE;
  4468.         /* fall through */
  4469.       case IcsObserving:
  4470.       case IcsPlayingWhite:
  4471.       case IcsPlayingBlack:
  4472.         pausing = TRUE;
  4473.         ModeHighlight();
  4474.         return;
  4475.       case PlayFromGameFile:
  4476.         (void) StopLoadGameTimer();
  4477.         pausing = TRUE;
  4478.         ModeHighlight();
  4479.         break;
  4480.       case BeginningOfGame:
  4481.         if (appData.icsActive) return;
  4482.         /* else fall through */
  4483.       case MachinePlaysWhite:
  4484.       case MachinePlaysBlack:
  4485.       case TwoMachinesPlay:
  4486.         if (forwardMostMove == 0)
  4487.           return;        /* don't pause if no one has moved */
  4488.         if ((gameMode == MachinePlaysWhite &&
  4489.          !WhiteOnMove(forwardMostMove)) ||
  4490.         (gameMode == MachinePlaysBlack &&
  4491.          WhiteOnMove(forwardMostMove))) {
  4492.         StopClocks();
  4493.         }
  4494.         pausing = TRUE;
  4495.         ModeHighlight();
  4496.         break;
  4497.     }
  4498.     }
  4499. }
  4500.  
  4501. void EditCommentEvent()
  4502. {
  4503.     char title[MSG_SIZ];
  4504.  
  4505.     if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
  4506.     strcpy(title, "Edit comment");
  4507.     } else {
  4508.     sprintf(title, "Edit comment on %d. %s%s", (currentMove - 1) / 2 + 1,
  4509.         WhiteOnMove(currentMove - 1) ? "" : "... ",
  4510.         parseList[currentMove - 1]);
  4511.     }
  4512.  
  4513.     EditCommentPopUp(currentMove, title, commentList[currentMove]);
  4514. }
  4515.  
  4516.  
  4517. void EditTagsEvent()
  4518. {
  4519.     char *tags = PGNTags(&gameInfo);
  4520.     EditTagsPopUp(tags);
  4521.     free(tags);
  4522. }
  4523.  
  4524.  
  4525. void MachineWhiteEvent()
  4526. {
  4527.     if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
  4528.       return;
  4529.     if (gameMode == PlayFromGameFile || gameMode == TwoMachinesPlay ||
  4530.     gameMode == EndOfGame)
  4531.       EditGameEvent();
  4532.     if (gameMode == EditPosition) EditPositionDone();
  4533.    
  4534.     if (!WhiteOnMove(gameMode == EditGame ? currentMove : forwardMostMove)) {
  4535.     DisplayError("It is not White's turn", 0);
  4536.     return;
  4537.     }
  4538.     if (gameMode == EditGame) forwardMostMove = currentMove;
  4539.     ResurrectChessProgram(); /* in case it isn't running */
  4540.  
  4541.     lastGameMode = gameMode = MachinePlaysWhite;
  4542.     pausing = FALSE;
  4543.     ModeHighlight();
  4544.     SetGameInfo();
  4545.     SendToProgram(appData.whiteString, firstProgramPR);
  4546.     StartClocks();
  4547. }
  4548.  
  4549. void MachineBlackEvent()
  4550. {
  4551.     if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
  4552.       return;
  4553.     if (gameMode == PlayFromGameFile || gameMode == TwoMachinesPlay ||
  4554.     gameMode == EndOfGame)
  4555.       EditGameEvent();
  4556.     if (gameMode == EditPosition) EditPositionDone();
  4557.     
  4558.     if (WhiteOnMove(gameMode == EditGame ? currentMove : forwardMostMove)) {
  4559.     DisplayError("It is not Black's turn", 0);
  4560.     return;
  4561.     }
  4562.     if (gameMode == EditGame) forwardMostMove = currentMove;
  4563.     ResurrectChessProgram(); /* in case it isn't running */
  4564.  
  4565.     lastGameMode = gameMode = MachinePlaysBlack;
  4566.     pausing = FALSE;
  4567.     ModeHighlight();
  4568.     SetGameInfo();
  4569.     SendToProgram(appData.blackString, firstProgramPR);
  4570.     StartClocks();
  4571. }
  4572.  
  4573.  
  4574. void TwoMachinesEvent()
  4575. {
  4576.     int i;
  4577.     
  4578.     if (appData.noChessProgram) return;
  4579.     
  4580.     switch (gameMode) {
  4581.       case TwoMachinesPlay:
  4582.     return;
  4583.       case MachinePlaysWhite:
  4584.       case MachinePlaysBlack:
  4585.       case BeginningOfGame:
  4586.       case PlayFromGameFile:
  4587.       case EndOfGame:
  4588.     EditGameEvent();
  4589.     if (gameMode != EditGame) return;
  4590.     break;
  4591.       case EditPosition:
  4592.     EditPositionDone();
  4593.     break;
  4594.       case EditGame:
  4595.       default:
  4596.     break;
  4597.     }
  4598.     
  4599.     forwardMostMove = currentMove;
  4600.     ResurrectChessProgram(); /* in case first program isn't running */
  4601.  
  4602.     InitChessProgram(appData.secondHost, appData.secondChessProgram,
  4603.              &secondProgramPR, &secondProgramISR, &secondSendTime);
  4604.     if (startedFromSetupPosition) {
  4605.     if (blackPlaysFirst) {
  4606.         SendToProgram("force\na3\n", secondProgramPR);
  4607.         SendBoard(secondProgramPR, boards[backwardMostMove]);
  4608.     } else {
  4609.         SendBoard(secondProgramPR, boards[backwardMostMove]);
  4610.         SendToProgram("force\n", secondProgramPR);
  4611.     }
  4612.     } else {
  4613.     SendToProgram("force\n", secondProgramPR);
  4614.     }
  4615.     for (i = backwardMostMove; i < forwardMostMove; i++) {
  4616.     SendToProgram(moveList[i], secondProgramPR);
  4617.     }
  4618.     lastGameMode = gameMode = TwoMachinesPlay;
  4619.     pausing = FALSE;
  4620.     ModeHighlight();
  4621.     SetGameInfo();
  4622.     firstMove = TRUE;
  4623.     if (WhiteOnMove(forwardMostMove))
  4624.       SendToProgram(appData.whiteString, secondProgramPR);
  4625.     else
  4626.       SendToProgram(appData.blackString, firstProgramPR);
  4627.     
  4628.     if (!firstSendTime || !secondSendTime) {
  4629.     ResetClocks();
  4630.     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
  4631.     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
  4632.     }
  4633.     StartClocks();
  4634. }
  4635.  
  4636.  
  4637. void IcsClientEvent()
  4638. {
  4639.     if (!appData.icsActive) return;
  4640.     switch (gameMode) {
  4641.       case IcsPlayingWhite:
  4642.       case IcsPlayingBlack:
  4643.       case IcsObserving:
  4644.       case IcsIdle:
  4645.       case BeginningOfGame:
  4646.       case IcsExamining:
  4647.     return;
  4648.  
  4649.       case EditGame:
  4650.     break;
  4651.  
  4652.       case EditPosition:
  4653.     EditPositionDone();
  4654.     break;
  4655.  
  4656.       default:
  4657.     EditGameEvent();
  4658.     break;
  4659.     }
  4660.  
  4661.     gameMode = IcsIdle;
  4662.     ModeHighlight();
  4663.     return;
  4664. }
  4665.  
  4666.  
  4667. void EditGameEvent()
  4668. {
  4669.     int i;
  4670.     char str[MSG_SIZ];
  4671.     
  4672.     switch (gameMode) {
  4673.       case MachinePlaysWhite:
  4674.     if (WhiteOnMove(forwardMostMove)) {
  4675.         DisplayError("Wait until your turn,\nor select Move Now", 0);
  4676.         return;
  4677.     }
  4678.     SendToProgram("force\n", firstProgramPR);
  4679.     break;
  4680.       case MachinePlaysBlack:
  4681.     if (!WhiteOnMove(forwardMostMove)) {
  4682.         DisplayError("Wait until your turn,\nor select Move Now", 0);
  4683.         return;
  4684.     }
  4685.     SendToProgram("force\n", firstProgramPR);
  4686.     break;
  4687.       case BeginningOfGame:
  4688.     SendToProgram("force\n", firstProgramPR);
  4689.     break;
  4690.       case PlayFromGameFile:
  4691.         (void) StopLoadGameTimer();
  4692.     if (gameFileFP != NULL) {
  4693.         gameFileFP = NULL;
  4694.     }
  4695.     break;
  4696.       case EditPosition:
  4697.     EditPositionDone();
  4698.     break;
  4699.       case TwoMachinesPlay:
  4700.     GameEnds((ChessMove) 0, NULL, GE_PLAYER);
  4701.     ResurrectChessProgram();
  4702.     break;
  4703.       case EndOfGame:
  4704.     ResurrectChessProgram();
  4705.     break;
  4706.       case IcsPlayingBlack:
  4707.       case IcsPlayingWhite:
  4708.     DisplayError("Aren't you playing a game?\nAdjourn or finish before you try to edit locally.", 0);
  4709.     return;
  4710.       case IcsObserving:
  4711.     SendToICS("observe\n");
  4712.     sprintf(str, "Aren't you observing game %d?\nAttempting to stop observing it.", ics_gamenum);
  4713.     DisplayError(str, 0);
  4714.     break;
  4715.       case IcsExamining:
  4716.     DisplayError("Aren't you examining a game?\nStop examining before you try to edit locally.", 0);
  4717.     return;
  4718.       case IcsIdle:
  4719.     break;
  4720.       case EditGame:
  4721.       default:
  4722.     return;
  4723.     }
  4724.     
  4725.     pausing = FALSE;
  4726.     StopClocks();
  4727.  
  4728.     if (gameMode == MachinePlaysWhite ||
  4729.     gameMode == MachinePlaysBlack ||
  4730.     gameMode == TwoMachinesPlay ||
  4731.     gameMode == PlayFromGameFile) {
  4732.     i = forwardMostMove;
  4733.     while (i > currentMove) {
  4734.         SendToProgram("undo\n", firstProgramPR);
  4735.         i--;
  4736.     }
  4737.     whiteTimeRemaining = timeRemaining[0][currentMove];
  4738.     blackTimeRemaining = timeRemaining[1][currentMove];
  4739.     DisplayBothClocks();
  4740.     if (whiteFlag || blackFlag) {
  4741.         whiteFlag = blackFlag = 0;
  4742.     }
  4743.     DisplayTitle("");
  4744.     }        
  4745.     
  4746.     lastGameMode = gameMode = EditGame;
  4747.     ModeHighlight();
  4748.     SetGameInfo();
  4749. }
  4750.  
  4751.  
  4752. void EditPositionEvent()
  4753. {
  4754.     if (gameMode == EditPosition) {
  4755.     EditGameEvent();
  4756.     return;
  4757.     }
  4758.     
  4759.     EditGameEvent();
  4760.     if (gameMode != EditGame) return;
  4761.     
  4762.     lastGameMode = gameMode = EditPosition;
  4763.     ModeHighlight();
  4764.     SetGameInfo();
  4765.     if (currentMove > 0)
  4766.       CopyBoard(boards[0], boards[currentMove]);
  4767.     
  4768.     blackPlaysFirst = !WhiteOnMove(currentMove);
  4769.     ResetClocks();
  4770.     currentMove = forwardMostMove = backwardMostMove = 0;
  4771. }
  4772.  
  4773. void EditPositionDone()
  4774. {
  4775.     startedFromSetupPosition = TRUE;
  4776.     SendToProgram(appData.initString, firstProgramPR);
  4777.     SendSearchDepth(firstProgramPR);
  4778.     if (blackPlaysFirst) {
  4779.     strcpy(moveList[0], "");
  4780.     strcpy(parseList[0], "");
  4781.     currentMove = forwardMostMove = backwardMostMove = 1;
  4782.     CopyBoard(boards[1], boards[0]);
  4783.     SendToProgram("force\na3\n", firstProgramPR);
  4784.     SendCurrentBoard(firstProgramPR);
  4785.     DisplayTitle("");
  4786.     } else {
  4787.     currentMove = forwardMostMove = backwardMostMove = 0;
  4788.     SendCurrentBoard(firstProgramPR);
  4789.     SendToProgram("force\n", firstProgramPR);
  4790.     DisplayTitle("");
  4791.     }
  4792.     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
  4793.     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
  4794.     lastGameMode = gameMode = EditGame;
  4795.     ModeHighlight();
  4796. }
  4797.  
  4798. void SetWhiteToPlayEvent()
  4799. {
  4800.     if (gameMode != EditPosition) return;
  4801.     blackPlaysFirst = FALSE;
  4802.     DisplayBothClocks(); /* works because currentMove is 0 */
  4803. }
  4804.  
  4805. void SetBlackToPlayEvent()
  4806. {
  4807.     if (gameMode != EditPosition) return;
  4808.     blackPlaysFirst = TRUE;
  4809.     currentMove = 1;    /* kludge */
  4810.     DisplayBothClocks();
  4811.     currentMove = 0;
  4812. }
  4813.  
  4814. void EditPositionMenuEvent(selection, x, y)
  4815.      ChessSquare selection;
  4816.      int x, y;
  4817. {
  4818.     char buf[MSG_SIZ];
  4819.  
  4820.     if (gameMode != EditPosition && gameMode != IcsExamining) return;
  4821.     switch (selection) {
  4822.       case ClearBoard:
  4823.     for (x = 0; x < BOARD_SIZE; x++)
  4824.       for (y = 0; y < BOARD_SIZE; y++) {
  4825.           if (gameMode == IcsExamining) {
  4826.           if (boards[currentMove][y][x] != EmptySquare) {
  4827.               sprintf(buf, "x@%c%c\n", 'a' + x, '1' + y);
  4828.               SendToICS(buf);
  4829.           }
  4830.           } else /* gameMode == EditPosition */ {
  4831.           boards[0][y][x] = EmptySquare;
  4832.           }
  4833.       }
  4834.     if (gameMode == EditPosition)
  4835.       DrawPosition(FALSE, boards[0]);
  4836.     break;
  4837.  
  4838.       case WhitePlay:
  4839.     SetWhiteToPlayEvent();
  4840.     break;
  4841.  
  4842.       case BlackPlay:
  4843.     SetBlackToPlayEvent();
  4844.     break;
  4845.  
  4846.       case EmptySquare:
  4847.     if (gameMode == IcsExamining) {
  4848.         sprintf(buf, "x@%c%c\n", 'a' + x, '1' + y);
  4849.         SendToICS(buf);
  4850.     } else /* gameMode == EditPosition */ {
  4851.         boards[0][y][x] = EmptySquare;
  4852.         DrawPosition(FALSE, boards[0]);
  4853.     }
  4854.     break;
  4855.  
  4856.       default:
  4857.     if (gameMode == IcsExamining) {
  4858.         sprintf(buf, "%c@%c%c\n",
  4859.             PieceToChar(selection), 'a' + x, '1' + y);
  4860.         SendToICS(buf);
  4861.     } else /* gameMode == EditPosition */ {
  4862.         boards[0][y][x] = selection;
  4863.         DrawPosition(FALSE, boards[0]);
  4864.     }
  4865.     break;
  4866.     }
  4867. }
  4868.  
  4869.  
  4870. void AcceptEvent()
  4871. {
  4872.     /* Accept a pending offer of any kind from opponent */
  4873.     
  4874.     if (appData.icsActive) {
  4875.     SendToICS("accept\n");
  4876.     } else if (cmailMsgLoaded) {
  4877.     if (currentMove == cmailOldMove &&
  4878.         commentList[cmailOldMove] != NULL &&
  4879.         StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
  4880.            "Black offers a draw" : "White offers a draw")) {
  4881.         TruncateGame();
  4882.         GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
  4883.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
  4884.     } else {
  4885.         DisplayError("There is no pending offer on this move", 0);
  4886.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
  4887.     }
  4888.     } else {
  4889.     /* Currently GNU Chess doesn't make offers at all */
  4890.     }
  4891. }
  4892.  
  4893. void DeclineEvent()
  4894. {
  4895.     /* Decline a pending offer of any kind from opponent */
  4896.     
  4897.     if (appData.icsActive) {
  4898.     SendToICS("decline\n");
  4899.     } else if (cmailMsgLoaded) {
  4900.     if (currentMove == cmailOldMove &&
  4901.         commentList[cmailOldMove] != NULL &&
  4902.         StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
  4903.            "Black offers a draw" : "White offers a draw")) {
  4904. #ifdef NOTDEF
  4905.         AppendComment(cmailOldMove, "Draw declined");
  4906.         DisplayComment(cmailOldMove - 1, "Draw declined");
  4907. #endif /*NOTDEF*/
  4908.     } else {
  4909.         DisplayError("There is no pending offer on this move", 0);
  4910.     }
  4911.     } else {
  4912.     /* Currently GNU Chess doesn't make offers at all */
  4913.     }
  4914. }
  4915.  
  4916. void CallFlagEvent()
  4917. {
  4918.     /* Call your opponent's flag (claim a win on time) */
  4919.     if (appData.icsActive) {
  4920.     SendToICS("flag\n");
  4921.     } else {
  4922.     switch (gameMode) {
  4923.       default:
  4924.         return;
  4925.       case MachinePlaysWhite:
  4926.         if (whiteFlag) {
  4927.         if (blackFlag)
  4928.           GameEnds(GameIsDrawn, "Both players ran out of time",
  4929.                GE_PLAYER);
  4930.         else
  4931.           GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
  4932.         } else {
  4933.         DisplayError("Your opponent is not out of time", 0);
  4934.         }
  4935.         break;
  4936.       case MachinePlaysBlack:
  4937.         if (blackFlag) {
  4938.         if (whiteFlag)
  4939.           GameEnds(GameIsDrawn, "Both players ran out of time",
  4940.                GE_PLAYER);
  4941.         else
  4942.           GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
  4943.         } else {
  4944.         DisplayError("Your opponent is not out of time", 0);
  4945.         }
  4946.         break;
  4947.     }
  4948.     }
  4949. }
  4950.  
  4951. void DrawEvent()
  4952. {
  4953.     /* Offer draw or accept pending draw offer from opponent */
  4954.     
  4955.     if (appData.icsActive) {
  4956.     /* Note: tournament rules require draw offers to be
  4957.        made after you make your move but before you punch
  4958.        your clock.  Currently ICS doesn't let you do that;
  4959.        instead, you immediately punch your clock after making
  4960.        a move, but you can offer a draw at any time. */
  4961.     
  4962.     SendToICS("draw\n");
  4963.     } else if (cmailMsgLoaded) {
  4964.     if (currentMove == cmailOldMove &&
  4965.         commentList[cmailOldMove] != NULL &&
  4966.         StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
  4967.            "Black offers a draw" : "White offers a draw")) {
  4968.         GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
  4969.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
  4970.     } else if (currentMove == cmailOldMove + 1) {
  4971.         char *offer = WhiteOnMove(cmailOldMove) ?
  4972.           "White offers a draw" : "Black offers a draw";
  4973.         AppendComment(currentMove, offer);
  4974.         DisplayComment(currentMove - 1, offer);
  4975.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
  4976.     } else {
  4977.         DisplayError("You must make your move before offering a draw", 0);
  4978.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
  4979.     }
  4980.     } else {
  4981.     /* Currently GNU Chess doesn't offer or accept draws */
  4982.     }
  4983. }
  4984.  
  4985. void AdjournEvent()
  4986. {
  4987.     /* Offer Adjourn or accept pending Adjourn offer from opponent */
  4988.     
  4989.     if (appData.icsActive) {
  4990.     SendToICS("adjourn\n");
  4991.     } else {
  4992.     /* Currently GNU Chess doesn't offer or accept Adjourns */
  4993.     }
  4994. }
  4995.  
  4996.  
  4997. void AbortEvent()
  4998. {
  4999.     /* Offer Abort or accept pending Abort offer from opponent */
  5000.     
  5001.     if (appData.icsActive) {
  5002.     SendToICS("abort\n");
  5003.     } else {
  5004.     GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
  5005.     }
  5006. }
  5007.  
  5008. void ResignEvent()
  5009. {
  5010.     /* Resign.  You can do this even if it's not your turn. */
  5011.     
  5012.     if (appData.icsActive) {
  5013.     SendToICS("resign\n");
  5014.     } else {
  5015.     switch (gameMode) {
  5016.       case MachinePlaysWhite:
  5017.         GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
  5018.         break;
  5019.       case MachinePlaysBlack:
  5020.         GameEnds(BlackWins, "White resigns", GE_PLAYER);
  5021.         break;
  5022.       case EditGame:
  5023.         if (cmailMsgLoaded) {
  5024.         TruncateGame();
  5025.         if (WhiteOnMove(cmailOldMove)) {
  5026.             GameEnds(BlackWins, "White resigns", GE_PLAYER);
  5027.         } else {
  5028.             GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
  5029.         }
  5030.         cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
  5031.         }
  5032.         break;
  5033.       default:
  5034.         break;
  5035.     }
  5036.     }
  5037. }
  5038.  
  5039.  
  5040. void StopObservingEvent()
  5041. {
  5042.     /* Stop observing current game */
  5043.     SendToICS("observe\n");
  5044. }
  5045.  
  5046. void StopExaminingEvent()
  5047. {
  5048.     /* Stop observing current game */
  5049.     SendToICS("unexamine\n");
  5050. }
  5051.  
  5052. void ForwardInner(target)
  5053.      int target;
  5054. {
  5055.     int limit;
  5056.  
  5057.     if (gameMode == EditPosition)
  5058.       return;
  5059.     
  5060.     if (gameMode == PlayFromGameFile && !pausing)
  5061.       PauseEvent();
  5062.     
  5063.     if (gameMode == IcsExamining && pausing)
  5064.       limit = pauseExamForwardMostMove;
  5065.     else
  5066.       limit = forwardMostMove;
  5067.  
  5068.     if (currentMove >= limit) {
  5069.     if (gameFileFP != NULL)
  5070.       (void) LoadGameOneMove();
  5071.     return;
  5072.     }
  5073.     
  5074.     if (target > limit) target = limit;
  5075.     if (gameMode == EditGame) {
  5076.     while (currentMove < target) {
  5077.         SendToProgram(moveList[currentMove++], firstProgramPR);
  5078.     }
  5079.     } else {
  5080.     currentMove = target;
  5081.     }
  5082.     
  5083.     if (gameMode == EditGame || gameMode == EndOfGame) {
  5084.     whiteTimeRemaining = timeRemaining[0][currentMove];
  5085.     blackTimeRemaining = timeRemaining[1][currentMove];
  5086.     }
  5087.     DisplayBothClocks();
  5088.     DisplayMove(currentMove - 1);
  5089.     DrawPosition(FALSE, boards[currentMove]);
  5090.     if (commentList[currentMove] != NULL) {
  5091.     DisplayComment(currentMove - 1, commentList[currentMove]);
  5092.     }
  5093. }
  5094.  
  5095.  
  5096. void ForwardEvent()
  5097. {
  5098.     if (gameMode == IcsExamining && !pausing) {
  5099.     SendToICS("forward\n");
  5100.     } else {
  5101.     ForwardInner(currentMove + 1);
  5102.     }
  5103. }
  5104.  
  5105. void ToEndEvent()
  5106. {
  5107.     if (gameMode == IcsExamining && !pausing) {
  5108.     SendToICS("forward 999999\n");
  5109.     } else {
  5110.     ForwardInner(forwardMostMove);
  5111.     }
  5112. }
  5113.  
  5114. void BackwardInner(target)
  5115.      int target;
  5116. {
  5117.     if ((currentMove <= backwardMostMove) || (gameMode == EditPosition))
  5118.       return;
  5119.     
  5120.     if (gameMode == PlayFromGameFile && !pausing)
  5121.       PauseEvent();
  5122.     
  5123.     if (gameMode == EditGame) {
  5124.     while (currentMove > target) {
  5125.         SendToProgram("undo\n", firstProgramPR);
  5126.         currentMove--;
  5127.     }
  5128.     } else {
  5129.     currentMove = target;
  5130.     }
  5131.     
  5132.     if (gameMode == EditGame || gameMode == EndOfGame) {
  5133.     whiteTimeRemaining = timeRemaining[0][currentMove];
  5134.     blackTimeRemaining = timeRemaining[1][currentMove];
  5135.     }
  5136.     DisplayBothClocks();
  5137.     DisplayMove(currentMove - 1);
  5138.     DrawPosition(FALSE, boards[currentMove]);
  5139.     if (commentList[currentMove] != NULL) {
  5140.     DisplayComment(currentMove - 1, commentList[currentMove]);
  5141.     }
  5142. }
  5143.  
  5144. void BackwardEvent()
  5145. {
  5146.     if (gameMode == IcsExamining && !pausing) {
  5147.     SendToICS("backward\n");
  5148.     } else {
  5149.     BackwardInner(currentMove - 1);
  5150.     }
  5151. }
  5152.  
  5153. void ToStartEvent()
  5154. {
  5155.     if (gameMode == IcsExamining && !pausing) {
  5156.     SendToICS("backward 999999\n");
  5157.     } else {
  5158.     BackwardInner(backwardMostMove);
  5159.     }
  5160. }
  5161.  
  5162. void RevertEvent()
  5163. {
  5164.     if (gameMode != IcsExamining) {
  5165.     DisplayError("You are not examining a game", 0);
  5166.     return;
  5167.     }
  5168.     if (pausing) {
  5169.     DisplayError("You can't revert while pausing", 0);
  5170.     return;
  5171.     }
  5172.     SendToICS("revert\n");
  5173. }
  5174.  
  5175. void RetractMoveEvent()
  5176. {
  5177.     switch (gameMode) {
  5178.       case MachinePlaysWhite:
  5179.       case MachinePlaysBlack:
  5180.     if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
  5181.         DisplayError("Wait until your turn,\nor select Move Now", 0);
  5182.         return;
  5183.     }
  5184.     if (forwardMostMove < 2) return;
  5185.     currentMove = forwardMostMove = forwardMostMove - 2;
  5186.     whiteTimeRemaining = timeRemaining[0][currentMove];
  5187.     blackTimeRemaining = timeRemaining[1][currentMove];
  5188.     DisplayBothClocks();
  5189.     DisplayMove(currentMove - 1);
  5190.     DrawPosition(FALSE, boards[currentMove]);
  5191.     SendToProgram("remove\n", firstProgramPR);
  5192.     break;
  5193.  
  5194.       case BeginningOfGame:
  5195.       default:
  5196.     break;
  5197.  
  5198.       case IcsPlayingWhite:
  5199.       case IcsPlayingBlack:
  5200.     if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
  5201.         SendToICS("takeback 2\n");
  5202.     } else {
  5203.         SendToICS("takeback 1\n");
  5204.     }
  5205.     break;
  5206.     }
  5207. }
  5208.  
  5209. void MoveNowEvent()
  5210. {
  5211.     switch (gameMode) {
  5212.       case MachinePlaysWhite:
  5213.     if (!WhiteOnMove(forwardMostMove)) {
  5214.         DisplayError("It is your turn", 0);
  5215.         return;
  5216.     }
  5217.     InterruptChildProcess(firstProgramPR);
  5218.     break;
  5219.       case MachinePlaysBlack:
  5220.     if (WhiteOnMove(forwardMostMove)) {
  5221.         DisplayError("It is your turn", 0);
  5222.         return;
  5223.     }
  5224.     InterruptChildProcess(firstProgramPR);
  5225.     break;
  5226.       case TwoMachinesPlay:
  5227.     if (WhiteOnMove(forwardMostMove)) {
  5228.         InterruptChildProcess(secondProgramPR);
  5229.     } else {
  5230.         InterruptChildProcess(firstProgramPR);
  5231.     }
  5232.     break;
  5233.       case BeginningOfGame:
  5234.       default:
  5235.     break;
  5236.     }
  5237. }
  5238.  
  5239. void TruncateGameEvent()
  5240. {
  5241.     EditGameEvent();
  5242.     if (gameMode != EditGame) return;
  5243.     TruncateGame();
  5244. }
  5245.  
  5246. void TruncateGame()
  5247. {
  5248.     if (forwardMostMove > currentMove) {
  5249.     if (gameInfo.resultDetails != NULL) {
  5250.         free(gameInfo.resultDetails);
  5251.         gameInfo.resultDetails = NULL;
  5252.         gameInfo.result = GameUnfinished;
  5253.     }
  5254.     forwardMostMove = currentMove;
  5255.     }
  5256. }
  5257.  
  5258. void HintEvent()
  5259. {
  5260.     if (appData.noChessProgram) return;
  5261.     switch (gameMode) {
  5262.       case MachinePlaysWhite:
  5263.     if (WhiteOnMove(forwardMostMove)) {
  5264.         DisplayError("Wait until your turn", 0);
  5265.         return;
  5266.     }
  5267.     break;
  5268.       case BeginningOfGame:
  5269.       case MachinePlaysBlack:
  5270.     if (!WhiteOnMove(forwardMostMove)) {
  5271.         DisplayError("Wait until your turn", 0);
  5272.         return;
  5273.     }
  5274.     break;
  5275.       default:
  5276.     DisplayError("No hint available", 0);
  5277.     return;
  5278.     }
  5279.     SendToProgram("hint\n", firstProgramPR);
  5280.     hintRequested = TRUE;
  5281. }
  5282.  
  5283. void BookEvent()
  5284. {
  5285.     if (appData.noChessProgram) return;
  5286.     switch (gameMode) {
  5287.       case MachinePlaysWhite:
  5288.     if (WhiteOnMove(forwardMostMove)) {
  5289.         DisplayError("Wait until your turn", 0);
  5290.         return;
  5291.     }
  5292.     break;
  5293.       case BeginningOfGame:
  5294.       case MachinePlaysBlack:
  5295.     if (!WhiteOnMove(forwardMostMove)) {
  5296.         DisplayError("Wait until your turn", 0);
  5297.         return;
  5298.     }
  5299.     break;
  5300.       case EditPosition:
  5301.     EditPositionDone();
  5302.     break;
  5303.       case TwoMachinesPlay:
  5304.     return;
  5305.       default:
  5306.     break;
  5307.     }
  5308.     SendToProgram("bk\n", firstProgramPR);
  5309.     bookOutput[0] = NULLCHAR;
  5310.     bookRequested = TRUE;
  5311. }
  5312.  
  5313. void AboutGameEvent()
  5314. {
  5315.     char *tags = PGNTags(&gameInfo);
  5316.     TagsPopUp(tags, CmailMsg());
  5317.     free(tags);
  5318. }
  5319.  
  5320. /* end button procedures */
  5321.  
  5322. void PrintPosition(fp, move)
  5323.      FILE *fp;
  5324.      int move;
  5325. {
  5326.     int i, j;
  5327.     
  5328.     for (i = BOARD_SIZE - 1; i >= 0; i--) {
  5329.     for (j = 0; j < BOARD_SIZE; j++) {
  5330.         fprintf(fp, "%c", PieceToChar(boards[move][i][j]));
  5331.         fputc(j == BOARD_SIZE - 1 ? '\n' : ' ', fp);
  5332.     }
  5333.     }
  5334.     if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
  5335.       fprintf(fp, "white to play\n");
  5336.     else
  5337.       fprintf(fp, "black to play\n");
  5338. }
  5339.  
  5340. void PrintOpponents(fp)
  5341.      FILE *fp;
  5342. {
  5343.     if (gameInfo.white != NULL) {
  5344.     fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
  5345.     } else {
  5346.     fprintf(fp, "\n");
  5347.     }
  5348. }
  5349.  
  5350. void SetGameInfo()
  5351. {
  5352.     char buf[MSG_SIZ];
  5353.  
  5354.     /* This routine is used only for certain modes */
  5355.     ClearGameInfo(&gameInfo);
  5356.     switch (gameMode) {
  5357.       case MachinePlaysWhite:
  5358.     gameInfo.event = StrSave("GNU Chess game");
  5359.     gameInfo.site = StrSave(HostName());
  5360.     gameInfo.date = PGNDate();
  5361.     gameInfo.round = StrSave("-");
  5362.     if (strcmp(appData.firstHost, "localhost") != 0) {
  5363.         sprintf(buf, "%s@%s",
  5364.             appData.firstChessProgram, appData.firstHost);
  5365.         gameInfo.white = StrSave(buf);
  5366.     } else {
  5367.         gameInfo.white = StrSave(appData.firstChessProgram);
  5368.     }
  5369.     gameInfo.black = StrSave(UserName());
  5370.     sprintf(buf, "%d/%ld", appData.movesPerSession, timeControl/1000);
  5371.     gameInfo.timeControl = StrSave(buf);
  5372.     break;
  5373.       case MachinePlaysBlack:
  5374.     gameInfo.event = StrSave("GNU Chess game");
  5375.     gameInfo.site = StrSave(HostName());
  5376.     gameInfo.date = PGNDate();
  5377.     gameInfo.round = StrSave("-");
  5378.     gameInfo.white = StrSave(UserName());
  5379.     sprintf(buf, "%s@%s", appData.firstChessProgram, appData.firstHost);
  5380.     if (strcmp(appData.firstHost, "localhost") != 0) {
  5381.         sprintf(buf, "%s@%s",
  5382.             appData.firstChessProgram, appData.firstHost);
  5383.         gameInfo.black = StrSave(buf);
  5384.     } else {
  5385.         gameInfo.black = StrSave(appData.firstChessProgram);
  5386.     }
  5387.     sprintf(buf, "%d/%ld", appData.movesPerSession, timeControl/1000);
  5388.     gameInfo.timeControl = StrSave(buf);
  5389.     break;
  5390.       case TwoMachinesPlay:
  5391.     gameInfo.event = StrSave("GNU Chess game");
  5392.     gameInfo.site = StrSave(HostName());
  5393.     gameInfo.date = PGNDate();
  5394.     gameInfo.round = StrSave("-");
  5395.     if (strcmp(appData.secondHost, "localhost") != 0) {
  5396.         sprintf(buf, "%s@%s",
  5397.             appData.secondChessProgram, appData.secondHost);
  5398.         gameInfo.white = StrSave(buf);
  5399.     } else {
  5400.         gameInfo.white = StrSave(appData.secondChessProgram);
  5401.     }
  5402.     if (strcmp(appData.firstHost, "localhost") != 0) {
  5403.         sprintf(buf, "%s@%s",
  5404.             appData.firstChessProgram, appData.firstHost);
  5405.         gameInfo.black = StrSave(buf);
  5406.     } else {
  5407.         gameInfo.black = StrSave(appData.firstChessProgram);
  5408.     }
  5409.     sprintf(buf, "%d/%ld", appData.movesPerSession, timeControl/1000);
  5410.     gameInfo.timeControl = StrSave(buf);
  5411.     break;
  5412.       case EditGame:
  5413.     gameInfo.event = StrSave("Edited game");
  5414.     gameInfo.site = StrSave(HostName());
  5415.     gameInfo.date = PGNDate();
  5416.     gameInfo.round = StrSave("-");
  5417.     gameInfo.white = StrSave("-");
  5418.     gameInfo.black = StrSave("-");
  5419.     break;
  5420.       case EditPosition:
  5421.     gameInfo.event = StrSave("Edited position");
  5422.     gameInfo.site = StrSave(HostName());
  5423.     gameInfo.date = PGNDate();
  5424.     gameInfo.round = StrSave("-");
  5425.     gameInfo.white = StrSave("-");
  5426.     gameInfo.black = StrSave("-");
  5427.     break;
  5428.       case IcsPlayingWhite:
  5429.       case IcsPlayingBlack:
  5430.       case IcsObserving:
  5431.       case IcsExamining:
  5432.     break;
  5433.       case PlayFromGameFile:
  5434.     gameInfo.event = StrSave("Game from non-PGN file");
  5435.     gameInfo.site = StrSave(HostName());
  5436.     gameInfo.date = PGNDate();
  5437.     gameInfo.round = StrSave("-");
  5438.     gameInfo.white = StrSave("?");
  5439.     gameInfo.black = StrSave("?");
  5440.     break;
  5441.       default:
  5442.     break;
  5443.     }
  5444. }
  5445.  
  5446. void ReplaceComment(index, text)
  5447.      int index;
  5448.      char *text;
  5449. {
  5450.     int len;
  5451.  
  5452.     while (*text == '\n') text++;
  5453.     len = strlen(text);
  5454.     while (len > 0 && text[len - 1] == '\n') len--;
  5455.  
  5456.     if (commentList[index] != NULL)
  5457.       free(commentList[index]);
  5458.  
  5459.     if (len == 0) {
  5460.     commentList[index] = NULL;
  5461.     return;
  5462.     }
  5463.     commentList[index] = (char *) malloc(len + 2);
  5464.     strncpy(commentList[index], text, len);
  5465.     commentList[index][len] = '\n';
  5466.     commentList[index][len + 1] = NULLCHAR;
  5467. }
  5468.  
  5469. void AppendComment(index, text)
  5470.      int index;
  5471.      char *text;
  5472. {
  5473.     int oldlen, len;
  5474.     char *old;
  5475.  
  5476.     while (*text == '\n') text++;
  5477.     len = strlen(text);
  5478.     while (len > 0 && text[len - 1] == '\n') len--;
  5479.  
  5480.     if (len == 0) return;
  5481.  
  5482.     if (commentList[index] != NULL) {
  5483.     old = commentList[index];
  5484.     oldlen = strlen(old);
  5485.     commentList[index] = (char *) malloc(oldlen + len + 2);
  5486.     strcpy(commentList[index], old);
  5487.     free(old);
  5488.     strncpy(&commentList[index][oldlen], text, len);
  5489.     commentList[index][oldlen + len] = '\n';
  5490.     commentList[index][oldlen + len + 1] = NULLCHAR;
  5491.     } else {
  5492.     commentList[index] = (char *) malloc(len + 2);
  5493.     strncpy(commentList[index], text, len);
  5494.     commentList[index][len] = '\n';
  5495.     commentList[index][len + 1] = NULLCHAR;
  5496.     }
  5497. }
  5498.  
  5499. void SendToProgram(message, pr)
  5500.      char *message;
  5501.      ProcRef pr;
  5502. {
  5503.     int count, outCount, error;
  5504.     char *which;
  5505.     char buf[MSG_SIZ];
  5506.  
  5507.     if (pr == NULL) return;
  5508.     Attention(pr);
  5509.     lastMsgPR = pr;
  5510.     which = (pr == firstProgramPR) ? "first" : "second";
  5511.     
  5512.     if (appData.debugMode)
  5513.       fprintf(debugFP, "Sending to %s: %s", which, message);
  5514.     
  5515.     count = strlen(message);
  5516.     outCount = OutputToProcess(pr, message, count, &error);
  5517.     if (outCount < count) {
  5518.     sprintf(buf, "Error writing to %s chess program", which);
  5519.     DisplayFatalError(buf, error, 1);
  5520.     }
  5521. }
  5522.  
  5523. void ReceiveFromProgram(isr, message, count, error)
  5524.      InputSourceRef isr;
  5525.      char *message;
  5526.      int count;
  5527.      int error;
  5528. {
  5529.     char *end_str, *name, *which;
  5530.     char buf[MSG_SIZ];
  5531.  
  5532.     if (count <= 0) {
  5533.     if (isr == firstProgramISR) {
  5534.         which = "first";
  5535.         name = appData.firstChessProgram;
  5536.     } else if (isr == secondProgramISR) {
  5537.         which = "second";
  5538.         name = appData.secondChessProgram;
  5539.     } else {
  5540.         return;
  5541.     }
  5542.     if (count == 0) {
  5543.         sprintf(buf,
  5544.             "Error: %s chess program (%s) exited unexpectedly",
  5545.             which, name);
  5546.         RemoveInputSource(isr);
  5547.         DisplayFatalError(buf, 0, -1); /* don't exit */
  5548.     } else {
  5549.         sprintf(buf,
  5550.             "Error reading from %s chess program (%s)",
  5551.             which, name);
  5552.         RemoveInputSource(isr);
  5553.         DisplayFatalError(buf, error, -1); /* don't exit */
  5554.     }
  5555.     GameEnds((ChessMove) 0, NULL, GE_PLAYER);
  5556.     return;
  5557.     }
  5558.     
  5559.     if ((end_str = strchr(message, '\r')) != NULL)
  5560.       *end_str = NULLCHAR;
  5561.     if ((end_str = strchr(message, '\n')) != NULL)
  5562.       *end_str = NULLCHAR;
  5563.     
  5564.     if (appData.debugMode) {
  5565.     fprintf(debugFP, "Received from %s: %s\n",
  5566.         isr == firstProgramISR ? "first" : "second", message);
  5567.     }
  5568.     HandleMachineMove(message, isr);
  5569. }
  5570.  
  5571.  
  5572. void SendSearchDepth(pr)
  5573.      ProcRef pr;
  5574. {
  5575.     char message[MSG_SIZ];
  5576.     
  5577.     if (appData.searchDepth <= 0) return;
  5578.     
  5579.     sprintf(message, "depth\n%d\nhelp\n", appData.searchDepth);
  5580.     /* note kludge: "help" command forces gnuchessx to print
  5581.        out something that ends with a newline. */
  5582.     SendToProgram(message, pr);
  5583. }
  5584.  
  5585. void SendTimeRemaining(pr)
  5586.      ProcRef pr;
  5587. {
  5588.     char message[MSG_SIZ];
  5589.     long time, otime;
  5590.  
  5591.     /* Note: this routine must be called when the clocks are stopped
  5592.        or when they have *just* been set or switched; otherwise
  5593.        it will be off by the time since the current tick started.
  5594.     */
  5595.     if (WhiteOnMove(forwardMostMove)) {
  5596.     time = whiteTimeRemaining / 10;
  5597.     otime = (blackTimeRemaining - ics_increment) / 10;
  5598.     } else {
  5599.     time = blackTimeRemaining / 10;
  5600.     otime = (whiteTimeRemaining - ics_increment) / 10;
  5601.     }
  5602.     if (time <= 0) time = 1;
  5603.     if (otime <= 0) otime = 1;
  5604.     
  5605.     sprintf(message, "time %ld\notim %ld\n", time, otime);
  5606.     SendToProgram(message, pr);
  5607. }
  5608.  
  5609. void ShowThinkingEvent(newState)
  5610.      int newState;
  5611. {
  5612.     if (newState == appData.showThinking) return;
  5613.     switch (gameMode) {
  5614.       case MachinePlaysWhite:
  5615.     if (WhiteOnMove(forwardMostMove)) {
  5616.         DisplayError("Wait until your turn", 0);
  5617.         return;
  5618.     }
  5619.     break;
  5620.       case BeginningOfGame:
  5621.       case MachinePlaysBlack:
  5622.     if (!WhiteOnMove(forwardMostMove)) {
  5623.         DisplayError("Wait until your turn", 0);
  5624.         return;
  5625.     }
  5626.     break;
  5627.       case EditPosition:
  5628.     EditPositionDone();
  5629.     break;
  5630.       case TwoMachinesPlay:
  5631.     return;
  5632.       default:
  5633.     break;
  5634.     }
  5635.     if (newState) {
  5636.     SendToProgram("post\n", firstProgramPR);
  5637.     } else {
  5638.     SendToProgram("nopost\n", firstProgramPR);
  5639.     thinkOutput[0] = NULLCHAR;
  5640.     }
  5641.     appData.showThinking = newState;
  5642. }
  5643.  
  5644. void DisplayMove(moveNumber)
  5645.      int moveNumber;
  5646. {
  5647.     char message[MSG_SIZ];
  5648.     char res[MSG_SIZ];
  5649.  
  5650.     if (moveNumber == forwardMostMove - 1 &&
  5651.     gameInfo.resultDetails != NULL) {
  5652.     if (gameInfo.resultDetails[0] == NULLCHAR) {
  5653.         sprintf(res, " %s", PGNResult(gameInfo.result));
  5654.     } else {
  5655.         sprintf(res, " {%s} %s",
  5656.             gameInfo.resultDetails, PGNResult(gameInfo.result));
  5657.     }
  5658.     } else {
  5659.     res[0] = NULLCHAR;
  5660.     }
  5661.     
  5662.     if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
  5663.     DisplayMessage(res, "");
  5664.     } else {
  5665.     sprintf(message, "%d. %s%s%s", moveNumber / 2 + 1,
  5666.         WhiteOnMove(moveNumber) ? "" : "... ",
  5667.         parseList[moveNumber], res);
  5668.     DisplayMessage(message,
  5669.                moveNumber == forwardMostMove - 1 ? thinkOutput : "");
  5670.     }
  5671. }
  5672.  
  5673. void DisplayComment(moveNumber, text)
  5674.      int moveNumber;
  5675.      char *text;
  5676. {
  5677.     char title[MSG_SIZ];
  5678.  
  5679.     if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
  5680.     strcpy(title, "Comment");
  5681.     } else {
  5682.     sprintf(title, "Comment on %d. %s%s", moveNumber / 2 + 1,
  5683.         WhiteOnMove(moveNumber) ? "" : "... ",
  5684.         parseList[moveNumber]);
  5685.     }
  5686.  
  5687.     CommentPopUp(title, text);
  5688. }
  5689.  
  5690. /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
  5691.  * might be busy thinking on our time.  It can be omitted if your
  5692.  * gnuchess is configured to stop thinking immediately on any user
  5693.  * input.  However, that gnuchess feature depends on the FIONREAD
  5694.  * ioctl, which does not work properly on some flavors of Unix.
  5695.  */
  5696. void Attention(pr)
  5697.      ProcRef pr;
  5698. {
  5699. #if defined(ATTENTION)
  5700.     if (appData.noChessProgram || (pr == NoProc)) return;
  5701.     switch (gameMode) {
  5702.       case MachinePlaysWhite:
  5703.       case MachinePlaysBlack:
  5704.       case TwoMachinesPlay:
  5705.       case IcsPlayingWhite:
  5706.       case IcsPlayingBlack:
  5707.     if (forwardMostMove > backwardMostMove + 1 && maybePondering) {
  5708.         if (appData.debugMode)
  5709.           fprintf(debugFP, "Interrupting %s\n",
  5710.               pr == firstProgramPR ? "first" : "second");
  5711.         InterruptChildProcess(pr);
  5712.         maybePondering = FALSE;
  5713.     }
  5714.     break;
  5715.       default:
  5716.     break;
  5717.     }
  5718. #endif /*ATTENTION*/
  5719. }
  5720.  
  5721. void CheckFlags()
  5722. {
  5723.     if (whiteTimeRemaining <= 0) {
  5724.     if (!whiteFlag) {
  5725.         whiteFlag = TRUE;
  5726.         if (appData.icsActive) {
  5727.         if (appData.autoCallFlag &&
  5728.             gameMode == IcsPlayingBlack && !blackFlag)
  5729.           SendToICS("flag\n");
  5730.         } else {
  5731.         if (blackFlag)
  5732.           DisplayTitle("Both flags fell");
  5733.         else
  5734.           DisplayTitle("White's flag fell");
  5735.         }
  5736.     }
  5737.     }
  5738.     if (blackTimeRemaining <= 0) {
  5739.     if (!blackFlag) {
  5740.         blackFlag = TRUE;
  5741.         if (appData.icsActive) {
  5742.         if (appData.autoCallFlag &&
  5743.             gameMode == IcsPlayingWhite && !whiteFlag)
  5744.           SendToICS("flag\n");
  5745.         } else {
  5746.         if (whiteFlag)
  5747.           DisplayTitle("Both flags fell");
  5748.         else
  5749.           DisplayTitle("Black's flag fell");
  5750.         }
  5751.     }
  5752.     }
  5753. }
  5754.  
  5755. void CheckTimeControl()
  5756. {
  5757.     if (!appData.clockMode || appData.icsActive ||
  5758.     gameMode == PlayFromGameFile || forwardMostMove == 0) return;
  5759.     /*
  5760.      * add time to clocks when time control is achieved
  5761.      */
  5762.     if ((forwardMostMove % (appData.movesPerSession * 2)) == 0) {
  5763.     whiteTimeRemaining += timeControl;
  5764.     blackTimeRemaining += timeControl;
  5765.     }
  5766. }
  5767.  
  5768. void DisplayBothClocks()
  5769. {
  5770.     DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
  5771.     DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
  5772. }
  5773.  
  5774.  
  5775. /* Timekeeping seems to be a portability nightmare.  I think everyone
  5776.    has ftime(), but I'm really not sure, so I'm including some ifdefs
  5777.    to use other calls if you don't.  Clocks will be less accurate if
  5778.    you have neither ftime nor gettimeofday.
  5779. */
  5780.  
  5781. /* Get the current time as a TimeMark */
  5782. void GetTimeMark(tm)
  5783.      TimeMark *tm;
  5784. {
  5785. #if HAVE_GETTIMEOFDAY
  5786.  
  5787.     struct timeval timeVal;
  5788.     struct timezone timeZone;
  5789.  
  5790.     gettimeofday(&timeVal, &timeZone);
  5791.     tm->sec = (long) timeVal.tv_sec; 
  5792.     tm->ms = (int) (timeVal.tv_usec / 1000L);
  5793.  
  5794. #else /*!HAVE_GETTIMEOFDAY*/
  5795. #if HAVE_FTIME
  5796.  
  5797. #include <sys/timeb.h>
  5798.     struct timeb timeB;
  5799.  
  5800.     ftime(&timeB);
  5801.     tm->sec = (long) timeB.time;
  5802.     tm->ms = (int) timeB.millitm;
  5803.  
  5804. #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
  5805.     tm->sec = (long) time(NULL);
  5806.     tm->ms = 0;
  5807. #endif
  5808. #endif
  5809. }
  5810.  
  5811. /* Return the difference in milliseconds between two
  5812.    time marks.  We assume the difference will fit in a long!
  5813. */
  5814. long SubtractTimeMarks(tm2, tm1)
  5815.      TimeMark *tm2, *tm1;
  5816. {
  5817.     return 1000L*(tm2->sec - tm1->sec) +
  5818.            (long) (tm2->ms - tm1->ms);
  5819. }
  5820.  
  5821.  
  5822. /*
  5823.  * Code to manage the game clocks.
  5824.  *
  5825.  * In tournament play, black starts the clock and then white makes a move.
  5826.  * We give the human user a slight advantage if he is playing white---the
  5827.  * clocks don't run until he makes his first move, so it takes zero time.
  5828.  * Also, we don't account for network lag, so we could get out of sync
  5829.  * with GNU Chess's clock -- but then, referees are always right.  
  5830.  */
  5831.  
  5832. static TimeMark tickStartTM;
  5833. static long intendedTickLength;
  5834.  
  5835. long NextTickLength(timeRemaining)
  5836.      long timeRemaining;
  5837. {
  5838.     long nominalTickLength, nextTickLength;
  5839.  
  5840.     if (timeRemaining > 0L && timeRemaining <= 1000L)
  5841.       nominalTickLength = 100L;
  5842.     else
  5843.       nominalTickLength = 1000L;
  5844.     nextTickLength = timeRemaining % nominalTickLength;
  5845.     if (nextTickLength <= 0) nextTickLength += nominalTickLength;
  5846.  
  5847.     return nextTickLength;
  5848. }
  5849.  
  5850. /* Stop clocks and reset to a fresh time control */
  5851. void ResetClocks() 
  5852. {
  5853.     (void) StopClockTimer();
  5854.     whiteTimeRemaining = timeControl;
  5855.     blackTimeRemaining = timeControl;
  5856.     if (whiteFlag || blackFlag) {
  5857.     DisplayTitle("");
  5858.     whiteFlag = blackFlag = FALSE;
  5859.     }
  5860.     DisplayBothClocks();
  5861. }
  5862.     
  5863. #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
  5864.  
  5865. /* Decrement running clock by amount of time that has passed */
  5866. void DecrementClocks()
  5867. {
  5868.     long timeRemaining;
  5869.     long lastTickLength, fudge;
  5870.     TimeMark now;
  5871.  
  5872.     if (!appData.clockMode) return;
  5873.     
  5874.     GetTimeMark(&now);
  5875.  
  5876.     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
  5877.  
  5878.     /* Fudge if we woke up a little too soon */
  5879.     fudge = intendedTickLength - lastTickLength;
  5880.     if (fudge < 0 || fudge > FUDGE) fudge = 0;
  5881.  
  5882.     if (WhiteOnMove(forwardMostMove)) {
  5883.     timeRemaining = whiteTimeRemaining -= lastTickLength;
  5884.     DisplayWhiteClock(whiteTimeRemaining - fudge,
  5885.               WhiteOnMove(currentMove));
  5886.     } else {
  5887.     timeRemaining = blackTimeRemaining -= lastTickLength;
  5888.     DisplayBlackClock(blackTimeRemaining - fudge,
  5889.               !WhiteOnMove(currentMove));
  5890.     }
  5891.     
  5892.     CheckFlags();
  5893.     
  5894.     tickStartTM = now;
  5895.     intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
  5896.     StartClockTimer(intendedTickLength);
  5897. }
  5898.  
  5899.     
  5900. /* A player has just moved, so stop the previously running
  5901.    clock and (if in clock mode) start the other one.
  5902.    We redisplay both clocks in case we're in ICS mode, because
  5903.    ICS gives us an update to both clocks after every move.
  5904. */
  5905. void SwitchClocks()
  5906. {
  5907.     long lastTickLength;
  5908.     TimeMark now;
  5909.  
  5910.     GetTimeMark(&now);
  5911.  
  5912.     if (StopClockTimer() && appData.clockMode) {
  5913.     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
  5914.     if (WhiteOnMove(forwardMostMove)) {
  5915.         whiteTimeRemaining -= lastTickLength;
  5916.     } else {
  5917.         blackTimeRemaining -= lastTickLength;
  5918.     }
  5919.     CheckFlags();
  5920.     }
  5921.     CheckTimeControl();
  5922.     if (!((gameMode == PlayFromGameFile) &&
  5923.       (matchMode || (appData.timeDelay == 0 && !pausing)))) {
  5924.     DisplayBothClocks();
  5925.     }
  5926.  
  5927.     if (!appData.clockMode) return;
  5928.  
  5929.     switch (gameMode) {
  5930.       case MachinePlaysBlack:
  5931.       case MachinePlaysWhite:
  5932.       case BeginningOfGame:
  5933.     if (pausing) return;
  5934.     break;
  5935.  
  5936.       case EditGame:
  5937.       case PlayFromGameFile:
  5938.       case IcsExamining:
  5939.     return;
  5940.  
  5941.       default:
  5942.     break;
  5943.     }
  5944.  
  5945.     tickStartTM = now;
  5946.     intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
  5947.       whiteTimeRemaining : blackTimeRemaining);
  5948.     StartClockTimer(intendedTickLength);
  5949. }
  5950.     
  5951.  
  5952. /* Stop both clocks */
  5953. void StopClocks()
  5954. {    
  5955.     long lastTickLength;
  5956.     TimeMark now;
  5957.  
  5958.     if (!StopClockTimer()) return;
  5959.     if (!appData.clockMode) return;
  5960.  
  5961.     GetTimeMark(&now);
  5962.  
  5963.     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
  5964.     if (WhiteOnMove(forwardMostMove)) {
  5965.     whiteTimeRemaining -= lastTickLength;
  5966.     DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
  5967.     } else {
  5968.     blackTimeRemaining -= lastTickLength;
  5969.     DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
  5970.     }
  5971.     CheckFlags();
  5972. }
  5973.     
  5974. /* Start clock of player on move.  Time may have been reset, so
  5975.    if clock is already running, stop and restart it. */
  5976. void StartClocks()
  5977. {
  5978.     (void) StopClockTimer(); /* in case it was running already */
  5979.     DisplayBothClocks();
  5980.     CheckFlags();
  5981.  
  5982.     if (!appData.clockMode) return;
  5983.  
  5984.     GetTimeMark(&tickStartTM);
  5985.     intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
  5986.       whiteTimeRemaining : blackTimeRemaining);
  5987.     StartClockTimer(intendedTickLength);
  5988. }
  5989.  
  5990. char *TimeString(ms)
  5991.      long ms;
  5992. {
  5993.     long second, minute, hour, day;
  5994.     char *sign = "";
  5995.     static char buf[32];
  5996.     
  5997.     if (ms > 0 && ms <= 900) {
  5998.     /* convert milliseconds to tenths, rounding up */
  5999.     sprintf(buf, " 0.%1ld ", (ms+99L)/100L);
  6000.     return buf;
  6001.     }
  6002.  
  6003.     /* convert milliseconds to seconds, rounding up */
  6004.     /* use floating point to avoid strangeness of integer division
  6005.        with negative dividends on many machines */
  6006.     second = (long) floor(((double) (ms + 999L)) / 1000.0);
  6007.  
  6008.     if (second < 0) {
  6009.     sign = "-";
  6010.     second = -second;
  6011.     }
  6012.     
  6013.     day = second / (60 * 60 * 24);
  6014.     second = second % (60 * 60 * 24);
  6015.     hour = second / (60 * 60);
  6016.     second = second % (60 * 60);
  6017.     minute = second / 60;
  6018.     second = second % 60;
  6019.     
  6020.     if (day > 0)
  6021.       sprintf(buf, " %s%ld:%02ld:%02ld:%02ld ",
  6022.           sign, day, hour, minute, second);
  6023.     else if (hour > 0)
  6024.       sprintf(buf, " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
  6025.     else
  6026.       sprintf(buf, " %s%2ld:%02ld ", sign, minute, second);
  6027.     
  6028.     return buf;
  6029. }
  6030.  
  6031.  
  6032. /*
  6033.  * This is necessary because some C libraries aren't ANSI C compliant yet.
  6034.  */
  6035. char *StrStr(string, match)
  6036.      char *string, *match;
  6037. {
  6038.     int i, length;
  6039.     
  6040.     length = strlen(match);
  6041.     
  6042.     for (i = strlen(string) - length; i >= 0; i--, string++)
  6043.       if (!strncmp(match, string, length))
  6044.     return string;
  6045.     
  6046.     return NULL;
  6047. }
  6048.  
  6049. #ifndef _amigados
  6050. int StrCaseCmp(s1, s2)
  6051.      char *s1, *s2;
  6052. {
  6053.     char c1, c2;
  6054.     
  6055.     for (;;) {
  6056.     c1 = ToLower(*s1++);
  6057.     c2 = ToLower(*s2++);
  6058.     if (c1 > c2) return 1;
  6059.     if (c1 < c2) return -1;
  6060.     if (c1 == NULLCHAR) return 0;
  6061.     }
  6062. }
  6063.  
  6064.  
  6065. int ToLower(c)
  6066.      int c;
  6067. {
  6068.     return isupper(c) ? tolower(c) : c;
  6069. }
  6070.  
  6071.  
  6072. int ToUpper(c)
  6073.      int c;
  6074. {
  6075.     return islower(c) ? toupper(c) : c;
  6076. }
  6077. #endif /* !_amigados    */
  6078.  
  6079. char *StrSave(s)
  6080.      char *s;
  6081. {
  6082.     char *ret;
  6083.  
  6084.     if ((ret = (char *) malloc(strlen(s) + 1))) {
  6085.     strcpy(ret, s);
  6086.     }
  6087.     return ret;
  6088. }
  6089.  
  6090. char *StrSavePtr(s, savePtr)
  6091.     char *s, **savePtr;
  6092. {
  6093.     if (*savePtr) {
  6094.     free(*savePtr);
  6095.     }
  6096.     if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
  6097.     strcpy(*savePtr, s);
  6098.     }
  6099.     return(*savePtr);
  6100. }
  6101.  
  6102. char *PGNDate()
  6103. {
  6104.     time_t clock;
  6105.     struct tm *tm;
  6106.     char buf[MSG_SIZ];
  6107.  
  6108.     clock = time((time_t *)NULL);
  6109.     tm = localtime(&clock);
  6110.     sprintf(buf, "%04d.%02d.%02d",
  6111.         tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
  6112.     return StrSave(buf);
  6113. }
  6114.  
  6115.  
  6116. char *PositionToFEN(move)
  6117.      int move;
  6118. {
  6119.     int i, j, fromX, fromY, toX, toY;
  6120.     int whiteToPlay;
  6121.     char buf[128];
  6122.     char *p, *q;
  6123.     int emptycount;
  6124.  
  6125.     whiteToPlay = (gameMode == EditPosition) ?
  6126.       !blackPlaysFirst : (move % 2 == 0);
  6127.     p = buf;
  6128.  
  6129.     /* Piece placement data */
  6130.     for (i = BOARD_SIZE - 1; i >= 0; i--) {
  6131.     emptycount = 0;
  6132.     for (j = 0; j < BOARD_SIZE; j++) {
  6133.         if (boards[move][i][j] == EmptySquare) {
  6134.         emptycount++;
  6135.         } else {
  6136.         if (emptycount > 0) {
  6137.             *p++ = '0' + emptycount;
  6138.             emptycount = 0;
  6139.         }
  6140.         *p++ = PieceToChar(boards[move][i][j]);
  6141.         }
  6142.     }
  6143.     if (emptycount > 0) {
  6144.         *p++ = '0' + emptycount;
  6145.         emptycount = 0;
  6146.     }
  6147.     *p++ = '/';
  6148.     }
  6149.     *(p - 1) = ' ';
  6150.  
  6151.     /* Active color */
  6152.     *p++ = whiteToPlay ? 'w' : 'b';
  6153.     *p++ = ' ';
  6154.  
  6155.     /* !!We don't keep track of castling availability, so fake it */
  6156.     q = p;
  6157.     if (boards[move][0][4] == WhiteKing) {
  6158.     if (boards[move][0][7] == WhiteRook) *p++ = 'K';
  6159.     if (boards[move][0][0] == WhiteRook) *p++ = 'Q';
  6160.     }
  6161.     if (boards[move][7][4] == BlackKing) {
  6162.     if (boards[move][7][7] == BlackRook) *p++ = 'k';
  6163.     if (boards[move][7][0] == BlackRook) *p++ = 'q';
  6164.     }        
  6165.     if (q == p) *p++ = '-';
  6166.     *p++ = ' ';
  6167.  
  6168.     /* En passant target square */
  6169.     if (move > backwardMostMove) {
  6170.     fromX = moveList[move - 1][0] - 'a';
  6171.     fromY = moveList[move - 1][1] - '1';
  6172.     toX = moveList[move - 1][2] - 'a';
  6173.     toY = moveList[move - 1][3] - '1';
  6174.     if (fromY == (whiteToPlay ? 6 : 1) &&
  6175.         toY == (whiteToPlay ? 4 : 3) &&
  6176.         boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
  6177.         fromX == toX) {
  6178.         /* 2-square pawn move just happened */
  6179.         *p++ = toX + 'a';
  6180.         *p++ = whiteToPlay ? '6' : '3';
  6181.     } else {
  6182.         *p++ = '-';
  6183.     }
  6184.     } else {
  6185.     *p++ = '-';
  6186.     }
  6187.  
  6188.     /* !!We don't keep track of halfmove clock for 50-move rule */
  6189.     strcpy(p, " 0 ");
  6190.     p += 3;
  6191.  
  6192.     /* Fullmove number */
  6193.     sprintf(p, "%d", (move / 2) + 1);
  6194.     
  6195.     return StrSave(buf);
  6196. }
  6197.  
  6198. Boolean ParseFEN(board, blackPlaysFirst, fen)
  6199.      Board board;
  6200.      int *blackPlaysFirst;
  6201.      char *fen;
  6202. {
  6203.     int i, j;
  6204.     char *p;
  6205.     int emptycount;
  6206.  
  6207.     p = fen;
  6208.  
  6209.     /* Piece placement data */
  6210.     for (i = BOARD_SIZE - 1; i >= 0; i--) {
  6211.     j = 0;
  6212.     while (j < BOARD_SIZE) {
  6213.         if (isdigit(*p)) {
  6214.         emptycount = *p++ - '0';
  6215.         if (j + emptycount > BOARD_SIZE) return FALSE;
  6216.         while (emptycount--) board[i][j++] = EmptySquare;
  6217.         } else {
  6218.         board[i][j++] = CharToPiece(*p++);
  6219.         }
  6220.     }
  6221.     if (*p != '/' && *p != ' ') return FALSE;
  6222.     p++;
  6223.     }
  6224.  
  6225.     /* Active color */
  6226.     switch (*p) {
  6227.       case 'w':
  6228.     *blackPlaysFirst = FALSE;
  6229.     break;
  6230.       case 'b': 
  6231.     *blackPlaysFirst = TRUE;
  6232.     break;
  6233.       default:
  6234.     return FALSE;
  6235.     }
  6236.     
  6237.     /* !!We ignore the rest of the FEN notation */
  6238.     return TRUE;
  6239. }
  6240.